Skip to content

Commit

Permalink
Merge pull request #3140 from artsy/mc-jones/fix-use-countDocuments
Browse files Browse the repository at this point in the history
fix: use collection.countDocuments instead of cursor.count
  • Loading branch information
joeyAghion authored Feb 7, 2024
2 parents d1223f7 + 14abd0a commit 71af07b
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/api/apps/articles/model/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const mongoFetch = (input, callback) => {
if (!count) {
return cb()
}
return cursor.count(cb)
return db.collection("articles").countDocuments(query, cb)
},
],
(err, results) => {
Expand Down Expand Up @@ -99,7 +99,7 @@ export const promisedMongoFetch = input => {
if (!count) {
return cb()
}
return cursor.count(cb)
return db.collection("articles").countDocuments(query, cb)
},
],
(err, results) => {
Expand Down
23 changes: 23 additions & 0 deletions src/api/apps/articles/test/model/index/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,27 @@ describe("Article", () => {
results.length.should.equal(1)
})
)))

describe("#mongoFetch", () =>
it("returns results, counts, and totals", done =>
fabricate(
"articles",
{ _id: new ObjectId("5086df098523e60002000018"), layout: "video" },
() =>
Article.mongoFetch(
{
count: true,
limit: 5,
offset: 0,
layout: "standard",
},
// @ts-ignore
(err, res) => {
res.results.length.should.equal(5)
res.total.should.equal(11)
res.count.should.equal(10)
done()
}
)
)))
})
2 changes: 1 addition & 1 deletion src/api/apps/authors/model.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ Joi = require '../../lib/joi'
(cb) -> cursor.toArray cb
(cb) ->
return cb() unless input.count
cursor.count cb
db.collection("authors").countDocuments query, cb
(cb) ->
return cb() unless input.count
db.collection("authors").count cb
Expand Down
2 changes: 1 addition & 1 deletion src/api/apps/channels/model.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ request = require 'superagent'
.skip(input.offset or 0)
async.parallel [
(cb) -> cursor.toArray cb
(cb) -> cursor.count cb
(cb) -> db.collection('channels').countDocuments query, cb
(cb) -> db.collection('channels').count cb
], (err, [channels, count, total]) =>
callback err, {
Expand Down
2 changes: 1 addition & 1 deletion src/api/apps/curations/model.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ OPTIONS = { allowUnknown: true, stripUnknown: false }
.skip(input.offset or 0)
async.parallel [
(cb) -> cursor.toArray cb
(cb) -> cursor.count cb
(cb) -> db.collection('curations').countDocuments(query, cb)
(cb) -> db.collection('curations').count cb
], (err, [curations, count, total]) =>
callback err, {
Expand Down
2 changes: 1 addition & 1 deletion src/api/apps/sections/model.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ querySchema = (->
.skip(input.offset or 0)
async.parallel [
(cb) -> cursor.toArray cb
(cb) -> cursor.count cb
(cb) -> db.collection('sections').countDocuments(query, cb)
(cb) -> db.collection('sections').count cb
], (err, [sections, count, total]) =>
callback err, {
Expand Down
2 changes: 1 addition & 1 deletion src/api/apps/tags/model.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ Joi = require '../../lib/joi'
(cb) -> cursor.toArray cb
(cb) ->
return cb() unless input.count
cursor.count cb
db.collection('tags').countDocuments(query, cb)
(cb) ->
return cb() unless input.count
db.collection('tags').count cb
Expand Down
2 changes: 1 addition & 1 deletion src/api/apps/verticals/model.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Joi = require '../../lib/joi'
(cb) -> cursor.toArray cb
(cb) ->
return cb() unless input.count
cursor.count cb
db.collection('verticals').countDocuments(query, cb)
(cb) ->
return cb() unless input.count
db.collection('verticals').count cb
Expand Down

0 comments on commit 71af07b

Please sign in to comment.