I have a schema with timestamps: true paramter. I want to insert documents but only if they do not already exist. If they do exists, no operation should occur (noop).
so I use $setOnInsert paramter as following
return Order.findByIdAndUpdate(
order._id,
{
$setOnInsert: order
},
{
upsert: true,
new: true
});
If I understood correctly this should only insert the doc if it does not already exists and otherwise perform no update.
But what happens is that the updatedAt timestamp gets updated whenever I run this operation.
How can I prevent this? Is this intended behaviour or have I chosen the wrong approach to "insert only if not exists"?