0

I have two collections as A and B and both having document as below :

collection A

{_id:id1
 name: Mohan
 age:25
}

collection B

{_id:id2
 name: sohan
 age:29
}

I want to merge this two collection A and B and expected output as below:

{_id:id1,
 name: Mohan,
 age:25},
{_id:id2,
 name: Sohan
 age:29
}

Any one knows how to merge this two collections? And I don't want to be create a new collection.

Neo-coder
  • 7,715
  • 4
  • 33
  • 52
Jitendra
  • 51
  • 3

1 Answers1

0

If you want to add all documents of collection1 to collection2 then you need to use foreach function as given below :

db.collection1.find().forEach(function(doc){db.collection2.save(doc)});

The collection2 will contains all document from collection1 as well as all of its own documents.

user3322141
  • 158
  • 6
  • 23
  • Thank you for quick response. I want to query the result not to save in collection2. Is any other option? – Jitendra Apr 29 '15 at 08:37