mongodb - 将文档作为子推送并更新父文档中的时间戳
问题描述
我有一份文件如下:
{
"_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]
}
}
);
推荐阅读
- esoteric-languages - Befunge 程序在 @ 处意外循环
- c++ - 为什么 int x{ y = 5 } 可能?
- html - 盒子定位包装
- php - DataFixtures phpunit symfony 类型的预期值改为“整数”
- reactjs - 常规函数中的 Redux 钩子(钩子规则)
- sql - SQL:聚合总和和过滤的聚合总和
- api - Laravel 6 单元测试响应 401
- java - 碎片内存泄漏
- angularjs - SpringBoot AngularJS webSocket 集成
- sql - 如何从 SQL SERVER 中的日期时间戳的时间部分创建一天的四分之一