0

I have a collection in MongoDB. And want to remove an item from array.

My "users" collection is an array of objects.

When I type:

db.users.find({"tasks.task_id" : "h58sjIdj3jJZ"}).pretty()

in mongo shell, I get this result:

{
  "_id" : ObjectId("5955b45b7a4bf40544019359"),
  "profile" : {
    "name" : "Morning bay",
    "email" : "[email protected]",
    "phone" : "+1-641-155-88-84",
    "description" : "Lorem ipsum dolor sit amet, consectetur adipisicing elit"
  },
  "tasks" : [
    {
      "task_id" : "h58sjIdj3jJY",
      "time" : "11:15 AM",
      "date" : "07/01/2017",
      "description" : "Make 1st call to John White"
    },
    {
      "task_id" : "h58sjIdj3jJZ",
      "time" : "14:30 PM",
      "date" : "07/09/2017",
      "description" : "Send certificate and make Another call to J.White"
    }
  ],
  "progress" : [
    {
      "isActive" : "",
      "description" : ""
    }
  ]
}

So, every item in my collection looks like this. And it's ok.

But now I want to remove one item from "tasks" array in one of users items.

I type:

db.users.update({}, {$pull : {"tasks" : {"task_id" : "h58sjIdj3jJZ"}}});

an get this result:

WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 0 })

I read documentation and examples in it look similar, so I don't understand where is an error.

3
  • It should works. What is your mongodb version ? Commented Jul 3, 2017 at 18:43
  • Can you add { multi:true } as 3rd argument of your update ? db.users.update({}, {$pull : {"tasks" : {"task_id" : "h58sjIdj3jJZ"}}}, { multi:true } ); Commented Jul 3, 2017 at 18:44
  • already tried, but it doesn't help Commented Jul 3, 2017 at 19:14

2 Answers 2

2

you're using {} in update search query what by default means find any document, and then you're modifying its tasks list {$pull : {"tasks" : {"task_id" : "h58sjIdj3jJZ"}}}.

So, probably another document is being modified (which may or may not contain task of id h58sjIdj3jJZ but in that case its irrelevant whether it has it or not)

You have 2 options: use:

db.users.update({}, {$pull : {"tasks" : {"task_id" : "h58sjIdj3jJZ"}}}, { multi: true });

what eventually touches every document in db,

OR

make search query more specific (provide id or look for item with task_id in tasks)

db.users.update({ "tasks.task_id": "h58sjIdj3jJZ"}, {$pull : {"tasks" : {"task_id" : "h58sjIdj3jJZ"}}});

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

1 Comment

Yes! I used db.users.update({ "tasks.task_id": "h58sjIdj3jJZ"}, {$pull : {"tasks" : {"task_id" : "h58sjIdj3jJZ"}}}) and it helped! Thank you!
0

I tested it on a MongoDB Terminal Online and your query correctly pulled the document.

Here is the output.

Are you experiencing this issue with the shell or using an ODM (like Mongoose)?

1 Comment

I use MongoJS instead of Mongoose. And yes, there is the same issue. It doesn't remove an item from tasks array.

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.