diff --git a/lib/datastore.js b/lib/datastore.js index f91ef53..f75705c 100644 --- a/lib/datastore.js +++ b/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