Query.prototype.countDocuments()
Parameters
[filter] «Object» mongodb selector
[callback] «Function» optional params are (error, count)
Returns:
- «Query» this
Specifies this query as a countDocuments()
query. Behaves like count()
, except it always does a full collection scan when passed an empty filter {}
.
There are also minor differences in how countDocuments()
handles $where
and a couple geospatial operators. versus count()
.
Passing a callback
executes the query.
This function triggers the following middleware.
countDocuments()
Example:
const countQuery = model.where({ 'color': 'black' }).countDocuments();
query.countDocuments({ color: 'black' }).count(callback);
query.countDocuments({ color: 'black' }, callback);
query.where('color', 'black').countDocuments(function(err, count) {
if (err) return handleError(err);
console.log('there are %d kittens', count);
});
The countDocuments()
function is similar to count()
, but there are a few operators that countDocuments()
does not support. Below are the operators that count()
supports but countDocuments()
does not, and the suggested replacement:
$where
:$expr
$near
:$geoWithin
with$center
$nearSphere
:$geoWithin
with$centerSphere
当前内容版权归 mongoosejs 或其关联方所有,如需对内容或内容相关联开源项目进行关注与资助,请访问 mongoosejs .