Skip to content

Commit

Permalink
Merge pull request #300 from bit-cmdr/feature/count-documents-and-est…
Browse files Browse the repository at this point in the history
…imated-count
  • Loading branch information
mathieudutour authored Apr 28, 2020
2 parents b3e6e49 + 2eecc1a commit 3f0e6ad
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 3 deletions.
17 changes: 15 additions & 2 deletions docs/collection/count.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# `collection.count`

[Mongo documentation <i class="fa fa-external-link" style="position: relative; top: 2px;" />](http://mongodb.github.io/node-mongodb-native/3.2/api/Collection.html#count)
[Mongo documentation <i class="fa fa-external-link" style="position: relative; top: 2px;" />](http://mongodb.github.io/node-mongodb-native/3.2/api/Collection.html#countDocuments)

Returns the count of documents that would match a `find()` query. The `collection.count()` method does not perform the `find()` operation but instead counts and returns the number of results that match a query.
Returns the count of documents that would match a `find()` query. The `collection.count()` method does not perform the `find()` operation but instead counts and returns the number of results that match a query. The method points to `collection.countDocuments()` in the mongo driver.

#### Arguments

Expand All @@ -21,4 +21,17 @@ A promise
```js
users.count({name: 'foo'})
users.count('id') // a bit useless but consistent with the rest of the API
users.count()
```

## Getting an estimated count

[Mongo documentation <i class="fa fa-external-link" style="position: relative; top: 2px;" />](http://mongodb.github.io/node-mongodb-native/3.2/api/Collection.html#estimatedDocumentCount)

If you need to get a fast order of magnitude of the count of *all* documents in your collection, you can use the `estimate` option.

> ⚠️ The `query` argument will be ignored.
```js
users.count({}, { estimate: true })
```
11 changes: 10 additions & 1 deletion lib/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,17 @@ Collection.prototype.count = function (query, opts, fn) {
opts = {}
}

if (typeof query === 'function') {
fn = query
query = {}
}

return this._dispatch(function count (args) {
return args.col.count(args.query, args.options)
const {estimate, ...options} = args.options
if (estimate) {
return args.col.estimatedDocumentCount(options)
}
return args.col.countDocuments(args.query, options)
})({options: opts, query: query, callback: fn}, 'count')
}

Expand Down
23 changes: 23 additions & 0 deletions test/collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,29 @@ test('count > should not ignore options', (t) => {
})
})

test('count > should count with no arguments', (t) => {
return users.count({ c: 'counting' }).then((count) => {
t.is(count, 0)
return users.insert({ c: 'counting' })
}).then(() => {
return users.count()
}).then((count) => {
t.is(count, 77)
})
})

test('count > should estimate count', (t) => {
return users.count({}, { estimate: true }).then((count) => {
t.is(count, 51)
})
})

test('count > should estimate count with options', (t) => {
return users.count({}, { estimate: true, maxTimeMS: 10000 }).then((count) => {
t.is(count, 51)
})
})

test.cb('count > callback', (t) => {
users.count({ a: 'counting' }, t.end)
})
Expand Down

0 comments on commit 3f0e6ad

Please sign in to comment.