I have a document
{
"_id": "62ac8190ddb08e6ee5f2c7dd",
"status": "NEW",
"vendor": "62ac8171ddb08e6ee5f2c7ca",
"productsInOrder": [
{
"product": "62ac8176ddb08e6ee5f2c7cd",
"amount": 1
},
{
"product": "62ac8181ddb08e6ee5f2c7d0",
"amount": 1
}
],
"createdAt": "2022-06-17T13:28:48.815Z",
"updatedAt": "2022-06-17T13:39:44.073Z",
"orderNumber": 82,
"__v": 2
}
And a query to update second element of productsInOrder array in the document
db.collection.findOneAndUpdate({
status: "NEW",
vendor: "62ac8171ddb08e6ee5f2c7ca",
"productsInOrder.product": "62ac8181ddb08e6ee5f2c7d0",
"productsInOrder.amount": {
$lte: 3
},
},
{
$inc: {
"productsInOrder.$.amount": 1
}
})
After executing the update query I expect product with id "62ac8181ddb08e6ee5f2c7d0" to be updated, i.e it's amount increased by 1
{
"_id": "62ac8190ddb08e6ee5f2c7dd",
"status": "NEW",
"vendor": "62ac8171ddb08e6ee5f2c7ca",
"productsInOrder": [
{
"product": "62ac8176ddb08e6ee5f2c7cd",
"amount": 1
},
{
"product": "62ac8181ddb08e6ee5f2c7d0",
"amount": 2 // increased by 1
}
],
"createdAt": "2022-06-17T13:28:48.815Z",
"updatedAt": "2022-06-17T13:39:44.073Z",
"orderNumber": 82,
"__v": 2
}
but instead product with id "62ac8176ddb08e6ee5f2c7cd" gets updated as seen here https://mongoplayground.net/p/UEU5LCAVjD0
I don't understand why positional operator selects the first element of the array instead of the element specified in find query
How can I update array element of the same id that is specified in find query in `"productsInOrder.product"?