I am trying to fuse/merge the information contained in two documents inside a collection, that contain different information and have only one key in common. This is an example:
{
"_id": <Object ...>
"fruit": "Apple"
"color": "Green"
"market": "Townhall"
}
{
"_id": <Object ...>
"fruit": "Banana"
"color": "Yellow"
"market": "Townhall"
}
{
"_id": <Object ...>
"fruit": "Orange"
"color": "Orange"
"market": "Hospital"
}
This is an example of one of the type of documents inside the collection. The other type of documents inside the collection are the following ones:
{
"_id": <Object ...>
"market: "Townhall"
"size": "Very big"
"floors": 3
"workers": 500
}
{
"_id": <Object ...>
"market: "Hospital"
"size": "Medium"
"floors": 1
"workers": 50
}
The idea is to fuse the information in both documents, grouping them by the key "market" so that the final result is this one or similar to this one:
{
"fruit": "Apple"
"color": "Green"
"market": "Townhall"
"floors": 3
"workers": 500
}
{
"fruit": "Banana"
"color": "Yellow"
"market": "Townhall"
"floors": 3
"workers": 500
}
{
"fruit": "Orange"
"color": "Orange"
"market": "Hospital"
"floors": 1
"workers": 50
}
I know I have to use db.collection.aggregate(...), but so far I know how to group the documents based on the field "market", but as the number of documents of fruits is higher than the number of documents of markets and i want to exclude fields such as the id or the size, i am a bit lost because i don't know if a simple $push will work. Any ideas?