insert - MongoDB copy object while changing parameters -
in mongo, what's best way copy record while changing property on it?
for example have user object , want copy while changing properties. (logically):
db.users.find({_id:"fred"}).foreach( function(r) { db.users.insert(r.copy("_id"="barney","age":25)) } );
of course there no "copy" function, that's i'm looking for: way copy document, while changing properties (of course key _id).
you don't have copy anything, example work fine:
db.users. find( { _id: "fred" } ). foreach( function (r) { r._id = "barney"; r.age = 25; db.so.insert( r ); } );
i don't quite understand why want though. makes more sense insert new document original fields again new "barney" document.
Comments
Post a Comment