目前表 A 具有两个 doc:
{
"id": 1,
"arr": []
},
{
"id": 2,
"arr": []
}
其中 arr
是一个 doc 数组,里面将存储预期的 doc:
{
"title": "xxx", // 值为字符串
"num": 1 // 值为整型
}
现在我想对表 A 做一个 FindOneAndUpdate
的操作,使其 id
为 1
并且 arr.title
为 A
的数据,其 arr.num
可以自增 1
,如果该数据不存在,则插入如下 doc:
{
"title": "A",
"num": 1
}
能否根据以下的操作进行填充达到上面的目的?
{
// filter
{
"id": 1,
// TODO
},
// update
{
// TODO
},
// option
{}
}
自问自答吧...
使用聚合管道进行更新: