在将键值对添加到具有相同名称的数组时遇到问题。有没有一种方法可以根据索引添加它?
我尝试在搜索查询中找到有效的关键字。但是更新将其添加到文档的底部
db.getCollection('siteLog').updateOne(
{
'site':'test1',
'items.suspiciousActivity.source': 'Account Added to Admin Group'
},
{$set:
{'items.suspiciousActivity.source.Account Added to Admin Group' : 'summary':'test1'}
})
应在底部添加另一个键值对
"items" : [
{
"suspiciousActivity" : {
"source" : "Account Added to Admin Group",
"count" : "2",
"summary" : "Test summary for this"
}
},
{
"suspiciousActivity" : {
"source" : "Palo Alto FW - Local Account Login",
"count" : "2",
"summary" : "Another test summary"
}
},
{
"suspiciousActivity" : {
"source" : "Suspicious Failed Logon Attempts",
"count" : "2",
"summary": "more summaries"
}
},
答案 0 :(得分:1)
您需要使用$
来获取正确的items
索引,如下所示:
db.getCollection('siteLog').updateOne({
'site': 'test1',
'items.suspiciousActivity.source': 'Account Added to Admin Group'
}, {
$set: {
'items.$.suspiciousActivity.summary': 'test1'
}
})