我正在尝试向 mongo 数组添加一个新文档,并且我要求其中一个字段是当前时间戳。这是用于字段级版本控制,但我不知道如何组合 $push 和 $currentDate 以获得我想要的结果。
有人能指出我正确的方向吗?
db.tmp.adviceReportingJourney.update(
{ _id : "5525f99be4b041151d51386e5525f99be4b041151d513870" },
{
$push: {
"$currentDate": {
"Conversation1MeetingCreated" : {
"vid" : 4,
"ts" : {"$type": "timestamp"},
"data" : 1428552213559
}
}
}
}
)
最佳答案
您可以使用您的编码语言日期。现在添加当前时间 ;-) 喜欢(我假设 java;-)):
db.tmp.adviceReportingJourney.update(
{ _id : "5525f99be4b041151d51386e5525f99be4b041151d513870" },
{
$push: {
"$currentDate": {
"Conversation1MeetingCreated" : {
"vid" : 4,
"ts" : {"$type": "timestamp"},
"data" : Date.now()
}
}
}
}
)
更新:运行 mongo > 3.0 时,您可以使用 $currentDate。来自文档 $currentDate它表明 $currentDate 仅适用于 db.collection.update()、db.collection.findAndModify()。
请参阅下面的示例以更新嵌入文档“Conversation1MeetingCreated”(您更新时间戳的位置)使用:
{
$currentDate: {
"Conversation1MeetingCreated.ts": { $type: "timestamp" }
},
$set: {
"Conversation1MeetingCreated.vid" : 4,
"Conversation1MeetingCreated.data": 1428552213559
}
}
希望对您有所帮助。
关于mongodb - 结合 mongo $push 和 $currentDate 以将时间包含在新的数组元素中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39444046/