gpt4 book ai didi

node.js - 如何使用 arrayFilters 更新数组中的元素

转载 作者:太空宇宙 更新时间:2023-11-04 00:04:23 24 4
gpt4 key购买 nike

假设文档如下:

{
"group_id": 1,
"group_token": "3bu48xp9ok5",
"group_passward": "0000",
"admin": "Forne",
"member": [
{
"mid": 1,
"Name": "Forne",
"Email": "xxx@xxx.com",
"Rank": "admin",
"sub": [
"test",
"xxx",
"Forne"
]
}
]
}

这是我的试用:

var collection = myDB.collection('test');
collection.update({"group_token":"3bu48xp9ok5"},{$pull:{"member.$[elem1].sub":"test"}}, {arrayFilters: [{"elem1.Email":"xxx@xxx.com"}],multi: true})

我如何使用“member.Email”:“xxx@xxx.com”更新元素以使其成为子[“xxx”,“Forne”]

但它打印:

  name: 'MongoError',
message: 'No array filter found for identifier \'elem1\' in path \'member.$[elem1].sub\'',
driver: true,
index: 0,
code: 2,
errmsg: 'No array filter found for identifier \'elem1\' in path \'member.$[elem1].sub\'' }

如何更新“sub”:[]...?

最佳答案

您可以使用$set$ .

db.getCollection('tests').update({
"group_token":"3bu48xp9ok5", "member.Email" : "xxx@xxx.com"
},
{
"$set": {"member.$.sub": ["hhh", "Cat"]}
})

文档之前:

{
"_id" : ObjectId("5bd2f41ed79cc5d8b1c62972"),
"group_id" : 1,
"group_token" : "3bu48xp9ok5",
"group_passward" : "0000",
"admin" : "Forne",
"member" : [
{
"mid" : 1,
"Name" : "Forne",
"Email" : "xxx@xxx.com",
"Rank" : "admin",
"sub" : [
"test",
"xxx",
"Forne"
]
},
{
"mid" : 1,
"Name" : "Forne",
"Email" : "yyy@yyy.com",
"Rank" : "admin",
"sub" : [
"test1",
"yyyy",
"Forte"
]
}
]
}

更新文档后:

{
"_id" : ObjectId("5bd2f41ed79cc5d8b1c62972"),
"group_id" : 1,
"group_token" : "3bu48xp9ok5",
"group_passward" : "0000",
"admin" : "Forne",
"member" : [
{
"mid" : 1,
"Name" : "Forne",
"Email" : "xxx@xxx.com",
"Rank" : "admin",
"sub" : [
"hhhh",
"hhhh",
"hhhh"
]
},
{
"mid" : 1,
"Name" : "Forne",
"Email" : "yyy@yyy.com",
"Rank" : "admin",
"sub" : [
"test1",
"yyyy",
"Forte"
]
}
]
}

关于node.js - 如何使用 arrayFilters 更新数组中的元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53004456/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com