跳过时间戳中间件以获取猫鼬中的某些更新 [英] Skip timestamps middleware for certain updates in Mongoose

查看:55
本文介绍了跳过时间戳中间件以获取猫鼬中的某些更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序使用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属性. 建议使用示例) >,因为据称该访问本机MongoDB驱动程序.所以我尝试了这个:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆