From the documentation I read you can do this:
db.people.update( { name:"Joe" }, { $set: { n : 1 } } );
( http://www.mongodb.org/display/DOCS/Updating )
Now I would like to set it to a dynamic value like a counter or expression:
var i = 0;
db.people.update( { name:"Joe" }, { $set: { n : $i++ } } );
db.people.update( { name:"Joe" }, { $set: { n : ${new Date()} } } );
Is this possible ?
I would also accept any solution that does not need to modify and save the full document.
mongo is an extended javascript shell with mongodb support, so you can do anything that you can do with plain javascript:
and
Update
ah so.. that’s not going to work. You have to update each document invidually! JS is executed before you pass that query to the database. Effectly you’re doing the following:
As I said, you have to update each document individually, but you can execute the whole thing on server-side with
db.eval( )to speed up things a little bit;)