Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made findAndModify() callbacks use findAndModifyWriteOpResult #159

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/tcoll.js
Original file line number Diff line number Diff line change
Expand Up @@ -786,13 +786,15 @@ tcoll.prototype.findAndModify = function (query, sort, doc, opts, cb) {
$doc = $doc || query;
$doc = self._tdb._cloneDeep($doc);
updater.update($doc,true);
if (_.isUndefined($doc._id))
if (_.isUndefined($doc._id)) {
$doc._id = new self._tdb.ObjectID();
}
self._put($doc, false, safe.sure(cb, function () {
cb(null,opts.new?c._projectFields($doc):{});
cb(null, opts.new ? { value: c._projectFields($doc), ok: 1 } : {});
}));
} else
} else {
cb();
}
} else {
self._get(res[0], false, safe.sure(cb, function (obj) {
var robj = (opts.new && !opts.remove) ? obj : self._tdb._cloneDeep(obj);
Expand All @@ -808,7 +810,7 @@ tcoll.prototype.findAndModify = function (query, sort, doc, opts, cb) {
udoc._id = obj._id;
// put will add it back to indexes
self._put(udoc, opts.remove?true:false, safe.sure(cb,function () {
cb(null,c._projectFields(robj));
cb(null, { value: c._projectFields(robj), ok: 1 });
}));
}));
}
Expand Down
34 changes: 17 additions & 17 deletions test/contrib/find_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -590,36 +590,36 @@ exports.shouldCorrectlyFindAndModifyDocument = function(configuration, test) {
// Test return new document on change
collection.insert({'a':1, 'b':2}, {w:1}, function(err, doc) {
// Let's modify the document in place
collection.findAndModify({'a':1}, [['a', 1]], {'$set':{'b':3}}, {'new':true}, function(err, updated_doc) {
test.equal(1, updated_doc.a);
test.equal(3, updated_doc.b);
collection.findAndModify({'a':1}, [['a', 1]], {'$set':{'b':3}}, {'new':true}, function(err, update_res) {
test.equal(1, update_res.value.a);
test.equal(3, update_res.value.b);

// Test return old document on change
collection.insert({'a':2, 'b':2}, {w:1}, function(err, doc) {
// Let's modify the document in place
collection.findAndModify({'a':2}, [['a', 1]], {'$set':{'b':3}}, {w:1}, function(err, result, object) {
test.equal(2, result.a);
test.equal(2, result.b);
collection.findAndModify({'a':2}, [['a', 1]], {'$set':{'b':3}}, {w:1}, function(err, update_res, object) {
test.equal(2, update_res.value.a);
test.equal(2, update_res.value.b);

// Test remove object on change
collection.insert({'a':3, 'b':2}, {w:1}, function(err, doc) {
// Let's modify the document in place
collection.findAndModify({'a':3}, [], {'$set':{'b':3}}, {'new': true, remove: true}, function(err, updated_doc) {
test.equal(3, updated_doc.a);
test.equal(2, updated_doc.b);
collection.findAndModify({'a':3}, [], {'$set':{'b':3}}, {'new': true, remove: true}, function(err, update_res) {
test.equal(3, update_res.value.a);
test.equal(2, update_res.value.b);

// Let's upsert!
collection.findAndModify({'a':4}, [], {'$set':{'b':3}}, {'new': true, upsert: true}, function(err, updated_doc) {
test.equal(4, updated_doc.a);
test.equal(3, updated_doc.b);
collection.findAndModify({'a':4}, [], {'$set':{'b':3}}, {'new': true, upsert: true}, function(err, update_res) {
test.equal(4, update_res.value.a);
test.equal(3, update_res.value.b);

// Test selecting a subset of fields
collection.insert({a: 100, b: 101}, {w:1}, function (err, ids) {
collection.findAndModify({'a': 100}, [], {'$set': {'b': 5}}, {'new': true, fields: {b: 1}}, function (err, updated_doc) {
test.equal(2, Object.keys(updated_doc).length);
test.equal(ids[0]['_id'].toHexString(), updated_doc._id.toHexString());
test.equal(5, updated_doc.b);
test.equal("undefined", typeof updated_doc.a);
collection.findAndModify({'a': 100}, [], {'$set': {'b': 5}}, {'new': true, fields: {b: 1}}, function (err, update_res) {
test.equal(2, Object.keys(update_res.value).length);
test.equal(ids[0]['_id'].toHexString(), update_res.value._id.toHexString());
test.equal(5, update_res.value.b);
test.equal("undefined", typeof update_res.value.a);
test.done();
});
});
Expand Down
12 changes: 6 additions & 6 deletions test/misc-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ describe('Misc', function () {
it('GH-19 Unset must clean key from object', function (done) {
db.collection("GH19", {}, safe.sure(done,function (_coll) {
_coll.insert({name:'Tony',age:'37'}, safe.sure(done, function () {
_coll.findAndModify({},{age:1},{$set: {name: 'Tony'}, $unset: { age: true }},{new:true},safe.sure(done, function (doc) {
assert(!_.includes(_.keys(doc),'age'));
_coll.findAndModify({},{age:1},{$set: {name: 'Tony'}, $unset: { age: true }},{new:true},safe.sure(done, function (result) {
assert(!_.includes(_.keys(result.value),'age'));
_coll.findOne({},{age:1},safe.sure(done, function (obj) {
assert(!_.includes(_.keys(obj),'age'));
done();
Expand Down Expand Up @@ -91,8 +91,8 @@ describe('Misc', function () {
it('GH-26-2 sort order can also be optional and undefined for findAndRemove', function (done) {
db.collection("GH26-2", {}, safe.sure(done,function (_coll) {
_coll.insert({name:'Tony',age:'37'}, safe.sure(done, function () {
_coll.findAndModify({},{age:1},{$set: {name: 'Tony'}, $unset: { age: true }},undefined,safe.sure(done, function (doc) {
assert(doc.age);
_coll.findAndModify({},{age:1},{$set: {name: 'Tony'}, $unset: { age: true }},undefined,safe.sure(done, function (result) {
assert(result.value.age);
done();
}))
}))
Expand All @@ -102,8 +102,8 @@ describe('Misc', function () {
it('GH-26-3 doc can be undefined if remote is true for findAndModify', function (done) {
db.collection("GH26-3", {}, safe.sure(done,function (_coll) {
_coll.insert({name:'Tony',age:'37'}, safe.sure(done, function () {
_coll.findAndModify({},{age:1},undefined,{remove:true},safe.sure(done, function (doc) {
assert(doc.age);
_coll.findAndModify({},{age:1},undefined,{remove:true},safe.sure(done, function (result) {
assert(result.value.age);
done();
}))
}))
Expand Down
4 changes: 2 additions & 2 deletions test/update-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ describe('Incremental update', function () {
})
it("#1 $setOnInsert on findAndUpdate",function (done) {
db.collection("setOnInsert", {}, safe.sure(done,function (_coll) {
_coll.findAndModify({_id:2},{},{$setOnInsert:{name:"John",sub:{gender:"male"}}},{upsert:true,new:true}, safe.sure(done, function (obj) {
assert.deepEqual(obj,{"_id":2,"name":"John","sub":{"gender":"male"}});
_coll.findAndModify({_id:2},{},{$setOnInsert:{name:"John",sub:{gender:"male"}}},{upsert:true,new:true}, safe.sure(done, function (result) {
assert.deepEqual(result.value,{"_id":2,"name":"John","sub":{"gender":"male"}});
done();
}))
}))
Expand Down