我有一个这样的收藏...
{
"_id": {
"$oid": "r3d4ct3d"
},
"name": "david doan",
"dateAdded": "2023-07-21T22:15:13.199Z",
"email": "[email protected]",
"state": "New York",
"tags": [
"tag 1",
"tag 2"
],
}
我想做的就是将字符串
tag 3
推到 tags
数组上,而不必离开 Compass 中的聚合管道。我不想创建一个应用程序只是为了插入字符串,所以 .update()
是不可能的。
如何在不为其构建整个应用程序的情况下做到这一点?
我尝试过使用
$set
,但这不允许我使用 $push
并且 $reduce
运算符对我来说太复杂了。 🤷🏼u200d♂️
我尝试过使用
$group
,但是我没有得到正确的投影,而且我不想仅仅为了添加标签而复制整个架构。
经过仔细检查,我发现您可以通过将数组设置为其自身的
$concatArrays
以及要推送到该数组上的值,将原始文档中的数组与新值连接起来。
[
{
'$set': {
'tags': {
'$concatArrays': [
'$tags', [
'test'
]
]
}
}
}, {
'$merge': {
'into': 'Contact'
}
}
]