1

Here is my collection:

{
"_id":"5b3385af20b7dc2b008ef5b9",
"name":"C",
"distances":[{"_id":"5b3460b05b2edc1bbcb0f362",
"distance":7,
"waypoint":"5b3385af20b7dc2b008ef5b9",
"status":"available"},
{"_id":"5b3460b05b2edc1bbcb0f361",
"distance":4,
"waypoint":"5b3460a15b2edc1bbcb0f360",
"status":"available"}],
"createdAt":"2018-06-27T12:40:15.457Z",
"updatedAt":"2018-06-27T12:57:50.191Z",
"__v":0
}

Let's focus on the distances array only, so which is:

"distances":[{"_id":"5b3460b05b2edc1bbcb0f362",
"distance":7,
"waypoint":"5b3385af20b7dc2b008ef5b9",
"status":"available"},
{"_id":"5b3460b05b2edc1bbcb0f361",
"distance":4,
"waypoint":"5b3460a15b2edc1bbcb0f360",
"status":"available"}]

What i want to do is, I want to delete object and update the distances array which have "waypoint":"5b3460a15b2edc1bbcb0f360"

so far I have tried:

Model.update( {'_id': model._id}, { $pullAll: {distances: [{'waypoint': req.body.id}] } });

this isn't working. Please suggest a way out.

1 Answer 1

3

You can use $pull of MongoDB

db.collection.update(
  { },
  { $pull: { distances:  { waypoint:  req.body.id} } },
)

{multi: true}: adding this in above query will delete all entries matching { waypoint: req.body.id}

Sign up to request clarification or add additional context in comments.

2 Comments

It didn't worked. WayPoint.update({},{ $pull: { distances: { waypoint: req.body.id} } }, { multi: true }); Maybe because the waypoint is an object inside distances array? don't we need to mention it in $pull ?
Ignore. I added call back to update function to see if there is any error, and now it is working fine :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.