findandmodify

Atomicity of findAndModify on embedded documents

你离开我真会死。 提交于 2019-12-24 01:44:49
问题 On mongodb manual there is an example for atomic operations on a single document. book = { _id: 123456789, title: "MongoDB: The Definitive Guide", available: 3, checkout: [ { by: "joe", date: ISODate("2012-10-15") } ] } The manual states that the below operation is atomic: db.books.findAndModify ( { query: { _id: 123456789, available: { $gt: 0 } }, update: { $inc: { available: -1 }, $push: { checkout: { by: "abc", date: new Date() } } } } ) My question is what would happen if available field

Is mongoDB's findAndModify “transaction-save”

匆匆过客 提交于 2019-12-23 02:51:21
问题 i know, there is no transaction support on mongo DB. But now i need to read an value of an document, increment by 1 and write the new value. Or - different way: Update an element and read the value at the same time. For this i like to use find and modify : http://www.mongodb.org/display/DOCS/findAndModify+Command this command updates an document and returns the value before updating. Is this happens in on (same like) transaction? The point is: is it possible that an other session updates the

findAndModify Error in mongodb - nodejs - error code 17287

纵饮孤独 提交于 2019-12-11 09:48:57
问题 I am getting following error : MongoError: exception: nextSafe(): { $err: "Can't canonicalize query: B adValue bad sort specification", code: 17287 } functions.getNextIndex = function(callback){ db.collection('counters').findAndModify( {_id:'productId'}, {$inc: {sequence_value:1}}, function(err,data){ if(!err) callback(data); else callback(err); }); } 回答1: It seems you are missing the "sort" argument in your query. Try something like: db.collection('counters').findAndModify( {_id:'productId'}

findAndModify - MongoError: exception: must specify remove or update

巧了我就是萌 提交于 2019-12-10 02:30:27
问题 Id like to update an array and return the doc. Is my findAndModify syntax correct? this.becomeFollower = function(title, username, callback){ "use strict" posts.findAndModify({ query: {"title":title, "roster":"yes"}, update: { "$addToSet": { "followers":username } }, new: true, upsert: true }, function(err, doc){ console.log('find and modified ' +doc); }); } I had no problem using this: posts.update({"title":title, "roster":"yes"}, { "$addToSet": { "followers":username } }, function(err,

findAndModify - MongoError: exception: must specify remove or update

梦想与她 提交于 2019-12-05 01:56:27
Id like to update an array and return the doc. Is my findAndModify syntax correct? this.becomeFollower = function(title, username, callback){ "use strict" posts.findAndModify({ query: {"title":title, "roster":"yes"}, update: { "$addToSet": { "followers":username } }, new: true, upsert: true }, function(err, doc){ console.log('find and modified ' +doc); }); } I had no problem using this: posts.update({"title":title, "roster":"yes"}, { "$addToSet": { "followers":username } }, function(err, roster){ "use strict" if(err) return callback(err, null); callback(err, roster); }); Check out the docs for