Can specify 2 or 3 args and still get desired results

pull/2/head
Louis Chatriot 10 years ago
parent 4f2b2ac2dd
commit 75ee7a967f
  1. 12
      lib/cursor.js
  2. 23
      lib/datastore.js

@ -58,7 +58,7 @@ Cursor.prototype._exec = function(callback) {
, res = [], added = 0, skipped = 0, self = this , res = [], added = 0, skipped = 0, self = this
, i, keys, key , i, keys, key
; ;
try { try {
for (i = 0; i < candidates.length; i += 1) { for (i = 0; i < candidates.length; i += 1) {
if (model.match(candidates[i], this.query)) { if (model.match(candidates[i], this.query)) {
@ -69,7 +69,7 @@ Cursor.prototype._exec = function(callback) {
} else { } else {
res.push(candidates[i]); res.push(candidates[i]);
added += 1; added += 1;
if (this._limit && this._limit <= added) { break; } if (this._limit && this._limit <= added) { break; }
} }
} else { } else {
res.push(candidates[i]); res.push(candidates[i]);
@ -83,7 +83,7 @@ Cursor.prototype._exec = function(callback) {
// Apply all sorts // Apply all sorts
if (this._sort) { if (this._sort) {
keys = Object.keys(this._sort); keys = Object.keys(this._sort);
// Sorting // Sorting
var criteria = []; var criteria = [];
for (i = 0; i < keys.length; i++) { for (i = 0; i < keys.length; i++) {
@ -101,11 +101,11 @@ Cursor.prototype._exec = function(callback) {
} }
return 0; return 0;
}); });
// Applying limit and skip // Applying limit and skip
var limit = this._limit || res.length var limit = this._limit || res.length
, skip = this._skip || 0; , skip = this._skip || 0;
res = res.slice(skip, skip + limit); res = res.slice(skip, skip + limit);
} }
@ -123,4 +123,4 @@ Cursor.prototype.exec = function () {
// Interface // Interface
module.exports = Cursor; module.exports = Cursor;

@ -377,12 +377,25 @@ Datastore.prototype.count = function(query, callback) {
* If no callback is passed, we return the cursor so that user can limit, skip and finally exec * If no callback is passed, we return the cursor so that user can limit, skip and finally exec
* @param {Object} query MongoDB-style query * @param {Object} query MongoDB-style query
*/ */
Datastore.prototype.find = function (query, callback) { Datastore.prototype.find = function (query, projection, callback) {
switch (arguments.length) {
case 1:
projection = {};
// callback is undefined, will return a cursor
break;
case 2:
if (typeof projection === 'function') {
callback = projection;
projection = {};
}
break;
}
var cursor = new Cursor(this, query, function(err, docs, callback) { var cursor = new Cursor(this, query, function(err, docs, callback) {
var res = [], i; var res = [], i;
if (err) { return callback(err); } if (err) { return callback(err); }
for (i = 0; i < docs.length; i += 1) { for (i = 0; i < docs.length; i += 1) {
res.push(model.deepCopy(docs[i])); res.push(model.deepCopy(docs[i]));
} }
@ -407,7 +420,7 @@ Datastore.prototype.findOne = function (query, callback) {
if (docs.length === 1) { if (docs.length === 1) {
return callback(null, model.deepCopy(docs[0])); return callback(null, model.deepCopy(docs[0]));
} else { } else {
return callback(null, null); return callback(null, null);
} }
}); });
@ -547,4 +560,6 @@ Datastore.prototype.remove = function () {
module.exports = Datastore; module.exports = Datastore;

Loading…
Cancel
Save