Skip to content

Commit

Permalink
Use cursor for counting documents
Browse files Browse the repository at this point in the history
  • Loading branch information
louischatriot committed Jan 26, 2014
1 parent 1625e79 commit 407a7c1
Showing 1 changed file with 10 additions and 21 deletions.
31 changes: 10 additions & 21 deletions lib/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 407a7c1

Please sign in to comment.