Hazem-Ben-Khalfallah
5/8/2013 - 7:02 PM

Update in mongo db

Update in mongo db

//Update only x,y fields without touching to the other attributs of a single document
db.products.update( { item: "book", qty: { $gt: 5 } }, { $set: { x: 6 }, $inc: { y: 5} } )

//Updates only x,y fields and removes other attributs of a a single document
db.products.update( { item: "book", qty: { $gt: 5 } }, { x: 6 , y: 5 } )

//Update multiple documents correspending to the filter 
db.products.update( { item: "book", qty: { $gt: 5 } }, { $set: { x: 6, y: 15 } },{ multi: true } )

//Update a a single document or insert a new one
b.products.update( { item: "magazine", qty: { $gt: 5 } }, { $set: { x: 25, y: 50 } }, { upsert: true } )

//save= insert if no _id else upsert
db.products.save( { item: "book", qty: 40 } ) //insert new document
db.products.save( { _id:100, item:"juice" } ) //upsert a document