My deeply nested array structure is like this:
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
}
Can anyone please help in writing a mongoose query to delete a specific photo object based on its id? I have tried the following query, but it is not working:
User.update(
{'_id': req.params.userId},
{'places._id': req.params.placesId },
{ $pull: { "photos" : { id: req.params.id } } },
false,
true
);
There are several problems with your query:
To remedy these problems, your update query should look something like this:
User.update(
{"_id": req.params.userId, "places._id": req.params.placesId },
{ $pull: { "places.$.photos" : { "_id": req.params.id } } },
false,
true
);
Note: your third and fourth parameters, the boolean values, do not seem to match the parameters required by the mongoose Update call ; however, I am not an expert with Mongoose and you may be using an older version in which that structure is correct.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.