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

Loading…
Cancel
Save