javascript - copyValue from another collection in MongoDb -


can copy field collection collection?

i want copy values of bar foo collection, don't want type filed, , want insert in foo e new _id e field (userid) ( use node.js)

collection bar {     "_id" : objectid("77777777ffffff9999999999"),     "type" : 0,     "name" : "default",     "index" : 1,     "layout" : "1",     }   collection foo {     "_id" : new object id, //    "type" : 0, no in collection     "userid" : objectid("77777777ffffff9999999911"),     "name" : "default",     "index" : 1,     "layout" : "1",     } 

i try db.bar.copyto("foo"); copy entire collection

actually best option. when don't want new field in collection, remove using $unset:

db.foo.update({ },{ "$unset": { "type": 1 } },false,true) 

that remove field documents in new collection in 1 statement.

in future releases 2.6 , upwards can using aggregate:

db.bar.aggregate([     { "$project": {         "userid" : 1         "name" : 1         "index" : 1         "layout" : 1     }},     { "$out": "foo" } ]) 

the new $out method sends output of aggregation statement collection.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -