Hi @haifisch,
Thanks for reaching out to MonogDB Community forums 
I would suggest you to use the aggregation pipeline with the updateOne method. This will allow you to conditionally update the updatedBy field only if there are matching orders.
You can refer to the following aggregation code snippet for reference:
async function updateOrderLocations(docId, orderIds, newLocation, userId) {
const dateNow = new Date();
const result = await OrderModel.updateOne(
{
docId,
orders: {
$elemMatch: {
id: { $in: orderIds },
isReceived: true
}
}
},
[
{
$set: {
orders: {
$map: {
input: '$orders',
as: 'order',
in: {
$cond: [
{
$and: [
{ $in: ['$$order.id', orderIds] },
{ $eq: ['$$order.isReceived', true] }
]
},
{
$mergeObjects: ['$$order', { location: newLocation }]
},
'$$order'
]
}
}
}
}
},
{
$set: {
updatedBy: {
id: userId,
at: dateNow
}
}
}
]
);
if (result.modifiedCount > 0) {
console.log('Document updated successfully');
} else {
console.log('No matching orders found, document not updated');
}
}
Also, please ensure that the above aggregation code is based on your sample document shared. Make sure you do thorough testing before implementing it in your production environment.
I hope it helps! In case you have any further questions, please feel free to reach out.
Best regards,
Kushagra
3 Likes