I am trying to extract specific data from a document in my collection that contains values stored in an array.
{
"name": "ABC",
"details": [
{"color": "red", "price": 20000},
{"color": "black", "price": 22000},
{"color": "blue", "price": 21000}
]
},
{
"name": "XYZ",
"details": [
{"color": "yellow", "price": 10000},
{"color": "black", "price": 12000},
{"color": "green", "price": 11000}
]
},
{
"name": "CBD",
"details": [
{"color": "red", "price": 30000},
{"color": "pink", "price": 32000},
{"color": "blue", "price": 31000}
]
}
I need help filtering the data for colors red and blue. The desired output is:
{"name": "ABC", "color": "red", "price": 20000},
{"name": "ABC", "color": "blue", "price": 21000},
{"name": "CBD", "color": "red", "price": 30000},
{"name": "CBD", "color": "blue", "price": 31000}
Can someone provide me with the MongoDB query or JS/TS code to achieve this?