diff --git a/lib/datastore.js b/lib/datastore.js index d2a1f7d..808adf3 100755 --- a/lib/datastore.js +++ b/lib/datastore.js @@ -476,12 +476,12 @@ Datastore.prototype.findOne = function (query, projection, callback) { /** * Update all docs matching query - * For now, very naive implementation (recalculating the whole database) * @param {Object} query * @param {Object} updateQuery * @param {Object} options Optional options * options.multi If true, can update multiple documents (defaults to false) * options.upsert If true, document is inserted if the query doesn't match anything + * options.returnUpdatedDocs Defaults to false, if true return as third argument the array of updated matched documents (even if no change actually took place) * @param {Function} cb Optional callback, signature: err, numReplaced, upsert (set to true if the update was in fact an upsert) * * @api private Use Datastore.update which has the same signature @@ -562,9 +562,16 @@ Datastore.prototype._update = function (query, updateQuery, options, cb) { } // Update the datafile - self.persistence.persistNewState(_.pluck(modifications, 'newDoc'), function (err) { + var updatedDocs = _.pluck(modifications, 'newDoc'); + self.persistence.persistNewState(updatedDocs, function (err) { if (err) { return callback(err); } - return callback(null, numReplaced); + if (!options.returnUpdatedDocs) { + return callback(null, numReplaced); + } else { + var updatedDocsDC = []; + updatedDocs.forEach(function (doc) { updatedDocsDC.push(model.deepCopy(doc)); }); + return callback(null, numReplaced, updatedDocsDC); + } }); } ]); diff --git a/lib/indexes.js b/lib/indexes.js index 79227ce..4f6e736 100755 --- a/lib/indexes.js +++ b/lib/indexes.js @@ -20,7 +20,7 @@ function projectForUnique (elt) { if (typeof elt === 'boolean') { return '$boolean' + elt; } if (typeof elt === 'number') { return '$number' + elt; } if (util.isArray(elt)) { return '$date' + elt.getTime(); } - + return elt; // Arrays and objects, will check for pointer equality } diff --git a/test/db.test.js b/test/db.test.js index 496e71f..90996c0 100755 --- a/test/db.test.js +++ b/test/db.test.js @@ -1477,6 +1477,40 @@ describe('Database', function () { }); }); + it("If options.returnUpdatedDocs is true, return all matched docs", function (done) { + d.insert([{ a: 4 }, { a: 5 }, { a: 6 }], function (err, docs) { + docs.length.should.equal(3); + + d.update({ a: 7 }, { $set: { u: 1 } }, { multi: true, returnUpdatedDocs: true }, function (err, num, updatedDocs) { + num.should.equal(0); + updatedDocs.length.should.equal(0); + + d.update({ a: 5 }, { $set: { u: 2 } }, { multi: true, returnUpdatedDocs: true }, function (err, num, updatedDocs) { + num.should.equal(1); + updatedDocs.length.should.equal(1); + updatedDocs[0].a.should.equal(5); + updatedDocs[0].u.should.equal(2); + + d.update({ a: { $in: [4, 6] } }, { $set: { u: 3 } }, { multi: true, returnUpdatedDocs: true }, function (err, num, updatedDocs) { + num.should.equal(2); + updatedDocs.length.should.equal(2); + updatedDocs[0].u.should.equal(3); + updatedDocs[1].u.should.equal(3); + if (updatedDocs[0].a === 4) { + updatedDocs[0].a.should.equal(4); + updatedDocs[1].a.should.equal(6); + } else { + updatedDocs[0].a.should.equal(6); + updatedDocs[1].a.should.equal(4); + } + + done(); + }); + }); + }); + }); + }); + }); // ==== End of 'Update' ==== //