'MongoDB Merge Equivalent Collections
I have two collections with same structure and want to merge them in aggregation result and query & sort over them after merging.
E.g.;
First collection:
{_id: "123", "name": "sercan"}
Second collection:
{_id: "456", "name": "hakan"}
What I want;
[{_id: "123", "name": "sercan"}, {_id: "456", "name": "hakan"}]
What I tried;
{"from":"secondCollection",pipeline: [],"as":"seconds"}
// result
{_id: "123", "name": "sercan", seconds: [{_id: "456", "name": "hakan"}]}
And the above trial, puts all documents into seconds if there're more documents in the second collection.
Thanks in advance
Solution 1:[1]
The one option I have used is $merge
in MongoDB.
It's basically merge one collection to another and create new output collection if it does not exist.
Reference:
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Tyler2P |