-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
timestamps: updatedAt after findByIdAndUpdate #4768
Comments
Set the |
Thanks for pointing this out. However this was not my issue, I just noticed that my example was too minimal in that regard. Apologies. The "problem" seems to be, that mongoose does not update the
In contrast, saving the document via its
Is this the expected behavior? |
Yeah this is expected, when you use |
Alright, thank you for clarifying! |
@vkarpov15 is there a way to tell mongoose always overwrite timestamps? |
@viktornord unfortunately not. We'll consider adding an option for this for a future release. |
if findByIdAndUpdate doesn't trigger updateAt field update , i don't see any point to use the build in timestamp. |
@huangxuewu it does trigger |
@vkarpov15 somehow it is not updating in my code. I have to add const Announcement = new Schema({
is anything wrong with my code? |
What does |
Looking into this more closely, this is actually a bug rather than a feature we need to opt in to. That's because in OP's example, var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test');
var MyModel = mongoose.model('mySchema', new mongoose.Schema({
name: String
}, {
timestamps: true
}));
mongoose.set('debug', true);
new MyModel({name: 'boob'}).save(function(err, doc) {
console.log(doc.updatedAt);
setTimeout(function() {
doc = doc.toObject();
doc.name = 'bob';
// Add `$set`, now `updatedAt` gets bumped
MyModel.findByIdAndUpdate(doc._id, { $set: doc }, {new: true}, function(err, doc) {
console.log(doc.updatedAt); // should by +5 seconds, shouldn't it?
});
}, 1000);
}); The code is there that sets |
I wanted to replace the 3rd party mongoose-timestamp plugin through native mongoose functionality and specified the
timestamps
schema option.In contrast to the mentioned plugin however, mongoose does not update when invoking
findByIdAndUpdate
. Is this by design? I've often stumbled over the differences between document and query middleware, so I'm quite used to this now, but I feel, that timestamps should logically update whenever a document is updated.To make sure that I'm not doing anything wrong, here's a minimal example:
Would appreciate any feedback. If this behavior is really intentional, it should be stated in the docs, imho.
The text was updated successfully, but these errors were encountered: