I am new to MongoDB and unable to understand aggregate pipeline. I have a collection named Portfolio and here is a sample document:
{
"_id" : "fjkhjkdshjkhkdsgfsadhfghgdsa",
"Name" : "Alex's Portfolio",
"Stocks" : [
{
"StockId" : NumberInt(5454125454796),
"Quantity" : NumberInt(30),
"InvestedValue" : "4124"
},
{
"StockId" : NumberInt(5745454554541),
"Quantity" : NumberInt(6),
"InvestedValue" : "3048"
}
],
"MutualFunds" : [
{
"MutualFundId" : NumberInt(472546545646564),
"Quantity" : "12",
"InvestedValue" : "5233"
}
]
}
And I have 2 master collections named Stocks:
[{
"_id" : "d5043755-ff4d-47da-bbff-df838a79df3b",
"StockId" : NumberInt(5454125454796),
"Name" : "AB Industries Ltd.",
"LastPrice" : "5.70",
"LastPriceChange" : "0.27"
},
{
"_id" : "49224732-f380-4e6f-a933-6fdb5b42107f",
"StockId" : NumberInt(5745454554541),
"Name" : "AB Industries 2 Ltd.",
"LastPrice" : "5.70",
"LastPriceChange" : "0.27"
}]
& Mutual Funds:
[{
"_id" : "3d8a4baa-34e6-4e5b-b782-ec4eda213fc9",
"MfId" : NumberInt(472546545646564),
"Name" : "AB Industries Bond",
"LastNAV" : "1036.747",
"LastNAVChange" : "0.3298",
}]
I have tried using this as a reference, but didn't work in .NET Core 6 with MongoDB Driver.
Now, how can I achieve the below given JSON via aggregate pipeline:
{
"Name": "Alex's Portfolio",
"Securities": [
{
"StockId": 5454125454796,
"Name": "AB Industries Ltd.",
"LastPrice": 5.7,
"LastPriceChange": 0.27,
"Quantity": 30,
"InvestedValue": 4124
},
{
"StockId": 5745454554541,
"Name": "AB Industries 2 Ltd.",
"LastPrice": 5.7,
"LastPriceChange": 0.27,
"Quantity": 6,
"InvestedValue": 3048
}
],
"Mfs": [
{
"MfId": 472546545646564,
"Name": "AB Industries Bond",
"LastNAV": 1036.747,
"LastNAVChange": 0.3298,
"Quantity": 12,
"InvestedValue": 5233
}
]
}