How to merge two colletion in MongoDB of same type using single Query -
this question has answer here:
- how join 2 tables in mongodb 3 answers
- mongodb: combine data multiple collections one..how? 8 answers
i have 2 collections a
, b
, both having document below :
collection a
{_id:id1 name: mohan age:25 }
collection b
{_id:id2 name: sohan age:29 }
i want merge 2 collection a
, b
, expected output below:
{_id:id1, name: mohan, age:25}, {_id:id2, name: sohan age:29 }
any 1 knows how merge 2 collections? , don't want create new collection.
if want add documents of collection1 collection2 need use foreach function given below :
db.collection1.find().foreach(function(doc){db.collection2.save(doc)});
the collection2 contains document collection1 of own documents.
Comments
Post a Comment