跳过时间戳中间件以获取猫鼬中的某些更新 [英] Skip timestamps middleware for certain updates in Mongoose
问题描述
我的应用程序使用Mongoose,并且具有使用时间戳选项的模式:>
My application uses Mongoose and has a schema that uses the timestamps option:
var fooSchema = new Schema({
name: String,
}, {
timestamps: true,
});
mongoose.model('Foo', fooSchema);
因此,每当将更新写入集合时,updatedAt
属性就会更改为当前日期.但是现在我想添加一些更改,这些更改应该不更新updatedAt
属性.
建议使用
So whenever an update is written to the collection, the updatedAt
property is changed to the current date. But now I would like to add some changes that should not update the updatedAt
property.
Some answers on Stackoverflow (example) suggested to use Foo.collection
as that allegedly accesses the native MongoDB driver. So I tried this:
Foo.collection.update({ _id: someFooId }, { $set: { name: 'New Name' } });
但是,这也改变了updatedAt
属性.
However, that changed the updatedAt
property as well.
那么如何在不更改updatedAt
的情况下 更新文档?
So how can I update a document, without changing updatedAt
?
推荐答案
我刚刚找到了一个解决方案,它对我来说很完美.
I just found a solution and it works perfectly to me.
mongoose.connection.db.collection('player').updateOne(
{_id: mongoose.Types.ObjectId('56cb91sf34746f14678934ba')},
{$set: {name: 'Test'}}
);
此查询不会更新updatedAt
字段.希望你仍然需要这个!
This query will not update the updatedAt
field. Hope you still need this!
这篇关于跳过时间戳中间件以获取猫鼬中的某些更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!