如何在每次保存时使用 Mongoose 架构中的当前日期更新属性? [英] How do I update a property with the current date in a Mongoose schema on every save?

查看:17
本文介绍了如何在每次保存时使用 Mongoose 架构中的当前日期更新属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的数据库集合中,每次使用当前日期时间更新记录时,我都想更新lastChanged"字段.我希望它与猫鼬的默认日期格式相同:

In my database collections, I want to update a 'lastChanged' field every time the record is updated with the current datetime. I want it to be in the same format as mongoose's default date like:

ISODate("2011-10-06T14: 01: 31.106Z")

有什么好话吗?

推荐答案

如果您只想要一个 ISO 字符串,请使用:

If you just want an ISO String use:

new Date().toISOString()

这篇关于如何在每次保存时使用 Mongoose 架构中的当前日期更新属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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