MongoDB提供了一种在更新操作时由系统更新日期字段的方法:https://docs.mongodb.com/manual/reference/operator/update/currentDate/.插入操作有没有相同的东西?
如果你不想从代码处理这个问题,我可能会尝试做一些事情(我已经直接在mongo shell上执行了下面的代码):
如果你想使用$ currentDate,请使用upsert = true:
db.orders.update(
{"_id":ObjectId()},
{
$currentDate: {
createtime: true
}
},
{ upsert: true }
)
Run Code Online (Sandbox Code Playgroud)它现在将在app服务器上生成objectid而不是日期/时间(除非你使用raw命令).
直接使用新的时间戳或日期对象:
db.orders.insert(
"createtime": new Timestamp()
)
Run Code Online (Sandbox Code Playgroud)大多数驱动程序的问题是确保在mondodb服务器上创建新对象 - 而不是在运行代码的机器上创建.您希望驱动程序允许运行raw insert命令.
两者都将用于避免应用程序服务器计算机之间的时间差异/时间同步问题.
| 归档时间: |
|
| 查看次数: |
8405 次 |
| 最近记录: |