Use cursor for counting documents

pull/2/head
Louis Chatriot 11 years ago
parent 1625e79ec9
commit 407a7c1bac
  1. 31
      lib/datastore.js

@ -353,35 +353,24 @@ Datastore.prototype.insert = function () {
this.executor.push({ this: this, fn: this._insert, arguments: arguments });
};
/**
* Count all documents matching the query
* @param {Object} query MongoDB-style query
*
* @api private Use count
*/
Datastore.prototype._count = function(query, callback) {
var res = 0
, self = this
, candidates = this.getCandidates(query)
, i
;
Datastore.prototype.count = function(query, callback) {
var cursor = new Cursor(this, query, function(err, docs, callback) {
if (err) { return callback(err); }
return callback(null, docs.length);
});
try {
for (i = 0; i < candidates.length; i += 1) {
if (model.match(candidates[i], query)) {
res++;
}
}
} catch (err) {
return callback(err);
if (typeof callback === 'function') {
cursor.exec(callback);
} else {
return cursor;
}
return callback(null, res);
};
Datastore.prototype.count = function() {
this.executor.push({this: this, fn: this._count, arguments: arguments });
};
/**
* Find all documents matching the query

Loading…
Cancel
Save