I am querying data using the aggregate method in MongoDB, but I have a problem. Specifically, I want to replace the value of the price field (with the condition: if this field value is null) with the value of price_option field in the first item from the model field where stock_count is greater than 0. How can I do it?
My code to query collection with aggregate:
const doc = await ProductModel.aggregate([
...
{
$group: {
_id: '$_id',
price: { $first: "$price" },
model: {
$addToSet: {
price_option: '$model.priceOption',
currency_unit: '$model.currencyUnit',
stock_count: '$model.stockCount'
}
}
}
},
{
$project: {
_id: 0,
id: '$_id',
price: 1,
model: 1
}
}
])
My sample data created in collection:
{
"price": null,
"model": [
{
"price_option": "200",
"currency_unit": "dollar",
"stock_count": 5
},
{
"price_option": "350",
"currency_unit": "dollar",
"stock_count": 0
},
{
"price_option": "400",
"currency_unit": "dollar",
"stock_count": 2
},
]
}
]
My desired output is:
{
"price": "200", <= it would be replace by the first item of remaining items where stock_count greater than 0
"model": [
{
"price_option": "200",
"currency_unit": "dollar",
"stock_count": 5
},
{
"price_option": "350",
"currency_unit": "dollar",
"stock_count": 0
},
{
"price_option": "400",
"currency_unit": "dollar",
"stock_count": 2
},
]
}