问题描述
我有一个文档如下:
{
"_id": ObjectId("507f191e810c19729de860ea"),"updateTimestamp": Date(1234567),"actions": [
{
"timestamp": Date(123456)
"action": "FIRE"
},{
"timestamp": Date(1234567)
"action": "HIDE"
}
]
}
如何将子文档添加到 actions
数组并同时更新父文档的 updateTimestamp
字段?这个想法是我以后可以根据这些文档的最近操作活动对其进行排序,如 updateTimestamp
字段所反映的那样。
解决方法
就这么简单:
db.collection.updateOne(
{ _id: '[id here]'},{
$set: {
'updateTimestamp': '[timestampHere]'
},$push: {
'actions': [new record here]
}
}
);