gpt4 book ai didi

node.js - 如何在嵌套对象的嵌套数组中设置属性未定义

转载 作者:可可西里 更新时间:2023-11-01 10:36:24 28 4
gpt4 key购买 nike

{
_id: "xPBc4By8FemDwTPqH",
friends: [{
username: "michael",
friendid: "154879",
friendlist: [{
randid: 54685,
friendname: "john",
illustrations: [{
randid: 178432,
image: "friend1.jpg"
}, {
randid: 545457,
image: "friend2.jpg"
}]
}]
}]
}

我的数据库结构如下所示。我想在插图数组中提取特定图像。我试过这段代码:

Collections.user.update(
{ "friends.friendid": 154879} ,
{ $pull: { 'friends.$[].friendlist.$[].illustrations': { "image": req.body.image } } } ,
function(err,result) {
if (err)
console.log(err);
else
res.send("Deleted");
})

此查询有效,但会删除整个对象,而不是仅删除过滤后的图像。我尝试过的另一种选择是设置 image = undefined,但它也没有用。

有人可以帮帮我吗?

最佳答案

编辑:

自 3.6 版以来,MongoDB 包含了 filtered positional operator我认为这正是您所需要的:

Collections.user.update(
{ "friends.friendid": 154879 },
{ $pull: { "friends.$[elem1].friendlist.$[elem2].illustrations": { "image" : req.body.image } } },
{ arrayFilters: [
{ "elem1.friendlist.illustrations.image": req.body.image },
{ "elem2.illustrations.image": req.body.image }
]}
)

原始(和错误的)答案:

我认为这段代码可以工作:

Collections.user.update(
{ "friends.friendid": 154879 } ,
{ $pull: { "friends.friendlist.illustrations.image": req.body.image } } ,
function(err,result) {
if (err) console.log(err);
else res.send("Deleted");
}
);

关于node.js - 如何在嵌套对象的嵌套数组中设置属性未定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56460180/

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