I am trying to get the versionKey working in mongoose to protect against concurrent modification. I have constructed a test where I read in the account twice (account1 and account 2) modify account1 save it, modify account2 save it and expect a Version Number error.
it('check concurrent updates using the __v operator', function (done) {
Account.readAccount(localTestData.mumAccountId, function (err, account1) {
console.log('account1 with metadata : ', account1);
Account.readAccount(localTestData.mumAccountId, function (err, account2) {
console.log('account2 with metadata : ', account2);
//both accounts should be the same at this point.
account1.settings.annotationsDisplayed.push('account1 new text');
//account1.increment();
account1.save(function (err, account1saved) {
console.log('account1 after save -- version inc : ', account1saved);
account2.settings.annotationsDisplayed.push('account2 new text');
account2.save(function (err, account2saved) {
//shoould throw an error
console.log('account2 after save : ', account2saved);
console.log('account2 err : ', err);
expect(err).to.not.be(null);
done();
});
});
});
});
});
The above fails, and the last write wins.
The Mongoose debug shows the following for the first update :
Mongoose: accounts.update({ _id: ObjectId("54e747c59b5ff6153f000001") }) { '$inc': { __v: 1 }, '$pushAll': { 'settings.annotationsDisplayed': [ 'account1 new text' ] }, '$set': { lastModified: new Date("Fri, 20 Feb 2015 14:42:14 GMT") } } {}
and for the second :
Mongoose: accounts.update({ _id: ObjectId("54e747c59b5ff6153f000001") }) { '$inc': { __v: 1 }, '$pushAll': { 'settings.annotationsDisplayed': [ 'account2 new text' ] }, '$set': { lastModified: new Date("Fri, 20 Feb 2015 14:42:14 GMT") } } {}
I would expect the where clause on the update to include the __v field so the versionKey code can function, but not seeing it happen. I have read this blog http://aaronheckmann.tumblr.com/post/48943525537/mongoose-v3-part-1-versioning which seems to be the only real documentation but it is not helping at the moment. I am updating the array because that seems to force the increment based on the documentation, but if I manually call the increment method that also fails.
Any help much appreciated.