我深层嵌套的数组结构如下:
user {
"_id" : ObjectId("58d6aedc7b9785e422d89ab7"),
"email" : "blah",
"password" : "blah",
"username" : "blah",
"places" : [ {
"_id" : ObjectId("58d85c63d6270650fa05a674"),
"description" : "blah blah",
"country" : "blah",
"state" : "blah",
"latitude" : "-23",
"longitude" : "12",
"name" : "blah blah",
"photos" : [
{
"name" : "blah",
"photo_url" : "vhttp://blahblah.com",
"_id" : ObjectId("58d85c7ad6270650fa05a675")
},
{
"name" : "blah",
"photo_url" : "blah blah",
"_id" : ObjectId("58d85cc9d6270650fa05a676")
}
]
}]
"__v" : 0
}
任何人都可以帮助编写一个mongoose查询,根据其ID删除特定的照片对象吗?我尝试了以下查询,但它不起作用:
User.update(
{'_id': req.params.userId},
{'places._id': req.params.placesId },
{ $pull: { "photos" : { id: req.params.id } } },
false,
true
);
答案 0 :(得分:0)
您的查询存在以下问题:
要解决这些问题,您的更新查询应如下所示:
User.update(
{"_id": req.params.userId, "places._id": req.params.placesId },
{ $pull: { "places.$.photos" : { "_id": req.params.id } } },
false,
true
);
注意:您的第三个和第四个参数(布尔值)似乎与mongoose Update call所需的参数不匹配;但是,我不是Mongoose的专家,你可能正在使用其结构正确的旧版本。