having a document with the next structure(this is a document only with relevant paramaeters):
{
"_id": {
"$oid": "663021f4425fd6ef9f2a44f8"
},
"docId": "100",
"orders": [
{
"id": 1,
"isReceived": true,
"location": "TEST3",
},
{
"id": 2,
"isReceived": true,
"location": "TEST2",
},
{
"id": 3,
"isReceived": false
},
],
"updatedBy": {
"id": "1",
"at": {
"$date": "2024-05-28T23:00:16.104Z"
}
},
"createdBy": {
"id": 1,
"at": {
"$date": "2024-04-29T22:40:52.577Z"
}
},
"__v": 0
}
I want to update the locations of the orders given an array of orderIds and if isReceived is true and if docId is given, at the same time i want to update the parent document updatedBy with a new object if an order location is updated.
i came up with something like this using mongoose
model.findOneAndUpdate(
{
docId,
},
{
$set: {
'orders.$[element].location': "TEST",
updatedBy: { id:1, at: new Date()),
},
},
{
new: true,
arrayFilters: [
{
'element.orders': { $in: orders },
'element.isReceived': true,
},
],
},
);
docId and orders are given by the users, in this case could be 100 and [1,2,3].
my attempt works but it updates the updatedBy object even if you pass an array of orders that arent in the parent, in example: for docId = 100 and orders=[5], there is no order with id 5 but the updatedBy will be updated anyways and i dont want to register an update if no order was updated.
can anyone help me with this?.
thanks in advance