I have a nested array, I have to remove the one object , based upon the condition using aggregation
here is the JSON I get from mongodb
{
"_id": "633d275ceb34a28755974032",
"name": "free",
"products": [
{
"title": "Product 1",
"phone": [
{
"title": "Best Phone 1 ",
"video": {
"Location": "https://video.mp4"
},
"freePreview": true
}
]
},
{
"title": "Product 2",
"phone": [
{
"title": "Best Phone 2",
"video": {
"Location": "https://video.mp4"
},
"freePreview": false
}
]
}
]
}
But I need the data like
{
"_id": "633d275ceb34a28755974032",
"name": "free",
"products": [
{
"title": "Product 1",
"phone": [
{
"title": "Best Phone 1 ",
"video": {
"Location": "https://video.mp4"
},
"freePreview": true
}
]
},
{
"title": "Product 2",
"phone": [
{
"title": "Best Phone 2",
"freePreview": false
}
]
}
]
}
In this data "video object" is removed inside the phone array , because of freePreview is false,
Based upon the freePreview condition , help me to remove the video object
2
Answers
You need to use double $map operator to iterate through your array of arrays and use $cond along with
$$REMOVE
to remove value conditionally:Mongo Playground
Here’s a similar example.
@mickl’s answer works great. If you want to allow for possible document/schema changes, then rather than specifying the fields to keep explicitly, you could use
"$mergeObjects"
to keep everything and then only modify what you need.Borrowing heavily from @mickl, here’s how you could do that.
Try it on mongoplayground.net.