diff --git a/lib/cursor.js b/lib/cursor.js index 204ae4f..06944f0 100644 --- a/lib/cursor.js +++ b/lib/cursor.js @@ -48,6 +48,7 @@ Cursor.prototype.sort = function(sortQuery) { /** * Get all matching elements + * Will return pointers to matched elements (shallow copies), returning full copies is the role of find or findOne * This is an internal function, use exec which uses the executor * * @param {Function} callback - Signature: err, results @@ -66,12 +67,12 @@ Cursor.prototype._exec = function(callback) { if (this._skip && this._skip > skipped) { skipped += 1; } else { - res.push(model.deepCopy(candidates[i])); + res.push(candidates[i]); added += 1; if (this._limit && this._limit <= added) { break; } } } else { - res.push(model.deepCopy(candidates[i])); + res.push(candidates[i]); } } } diff --git a/lib/datastore.js b/lib/datastore.js index dd745d7..f91ef53 100644 --- a/lib/datastore.js +++ b/lib/datastore.js @@ -390,8 +390,14 @@ Datastore.prototype.count = function() { */ Datastore.prototype.find = function (query, callback) { var cursor = new Cursor(this, query, function(err, docs, callback) { + var res = [], i; + if (err) { return callback(err); } - return callback(null, docs); + + for (i = 0; i < docs.length; i += 1) { + res.push(model.deepCopy(docs[i])); + } + return callback(null, res); }); if (typeof callback === 'function') { @@ -410,7 +416,7 @@ Datastore.prototype.findOne = function (query, callback) { var cursor = new Cursor(this, query, function(err, docs, callback) { if (err) { return callback(err); } if (docs.length === 1) { - return callback(null, docs[0]); + return callback(null, model.deepCopy(docs[0])); } else { return callback(null, null); } diff --git a/test/db.test.js b/test/db.test.js index fe770cc..ca6ece6 100644 --- a/test/db.test.js +++ b/test/db.test.js @@ -645,7 +645,7 @@ describe('Database', function () { d.findOne({ a: { $gt: 14 } }).sort({ a: 1 }).skip(2).exec(function (err, doc) { assert.isNull(err); assert.isNull(doc); - + done(); }); });