I want to set the update timestamp only for updating the record (and insert timestamp for new record) in the upsert operation.
self.model.update({Id: obj.Id}, obj, {upsert: true}, function(err){
if (err) {
return;
}
//...
});
Is there anyway to handle this?
There is a pre.save middleware I could use maybe, but is there any way tell whether current operation is insert or update?
Thanks in advance!
You can't do it automatically. You can do it using $set: { timestamp: Date.now() }
on upsert.
There is a closed issue in mongoose bugtracker discussing this, here: defaults are ignored on upsert
A little late, but hope that helps someone!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With