order = await Order.findOneAndUpdate({_id: orderId}, {
$set: {
created_at: new Date(sold_date),
}
}, {
new: true
});
I'm trying to manually update a previously created created_at field in the document. It seems that mongo avoids this update and nothing happens (only for created_at). Below is the field definition in model:
const orderSchema = new Schema({
...
},
{timestamps: {createdAt: 'created_at', updatedAt: 'updated_at'}});
CodePudding user response:
You need to set timestamps
to false
in your findOneAndUpdate
. Unfortunately, it's still not updated in the documentation.
order = await Order.findOneAndUpdate({_id: orderId}, {
$set: {
created_at: new Date(sold_date),
}
}, {
new: true,
timestamps: false
});
CodePudding user response:
First you can Unset the field and then add the created_at field again.
order = await Order.findOneAndUpdate({_id: orderId}, {
{
$unset: {created_at: 1}
},
$set: {
created_at: new Date(sold_date),
}
}, {
new: true,
runValidators : true
});