简体   繁体   English

监听 mongoDB 中的更新操作并自动更新某些字段

[英]Listen for update operation in mongoDB and update certain field automatically

whenever any update operation occur on the collection, there should be an automated process in nodejs that will automatically update the timestamp for updateAt field.每当集合上发生任何更新操作时,nodejs 中都应该有一个自动化过程,它将自动更新updateAt字段的时间戳。

I don't want to do manually as there are multiple sources from it can be updated.我不想手动操作,因为可以更新多个来源。

MongoDB don't have any trigger, but there is a watch in MongoDB how can I use it.... I want to perform update operations inside the watch and don't want to trigger watch when we update it. MongoDB 没有任何触发器,但是 MongoDB 中有一个手表,我该如何使用它.... 我想在手表内部执行更新操作,不想在更新时触发手表。

You can use post update hooks described here But for this particular purpose, you can use { timestamps: true } in your mongoose schema and it will automatically update the createdAt and updatedAt fields您可以使用此处描述的更新后挂钩但出于此特定目的,您可以在 mongoose 架构中使用{ timestamps: true } ,它将自动更新 createdAt 和 updatedAt 字段

If you are not using mongoose take a look at this question .如果您不使用 mongoose 看看这个问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM