I am trying to delete an object from an array based on its _id field like so:
(posts is an array of objects)
User.findOneAndUpdate(
{ id: req.params.userid },
{ $pull: { 'posts': { 'posts._id': { $eq: req.body.postID } } } },
{ new: true }
)
However, it is deleting ALL posts in the array even though they have different _id values referenced by req.body.postID
.
Note: If i try this same query with a different field like the post's name, then it works just fine and deletes just that post. However I need to do this by _id field to ensure uniqueness.
Here is what the User
model looks like:
(I am not explicitly putting an _id field it is assigned one automatically)
let userSchema = new mongoose.Schema({
id: String,
displayName: String,
posts: [
{
url: String,
description: String,
likes: [String]
}
]
});
Why is this happening and what is a possible solution?