My existing data:
db.products.insertOne(
{
_id: 1,
sku: "abc123",
quantity: 10,
metrics: { orders: 2, ratings: 3.5 }
}
)
After that, I perform a subtraction on the quantity field.
db.products.updateOne(
{ sku: "abc123" },
{ $inc: { quantity: -20, "metrics.orders": 1 } }
)
My wanted result, minimum vaue of quantity to be zero:
{
_id: 1,
sku: 'abc123',
quantity: 0,
metrics: { orders: 3, ratings: 3.5 }
}
How can I achieve it using mongoose?