1

I am trying to remove values from nested array using mongoose in node js. My schema structure is like this:

    ([{_id: 1,


    results: [
      { item: "A", score: 5, answers: [ { q: 1, a: 4 }, { q: 2, a: 6 } ] },
      { item: "B", score: 8, answers: [ { q: 1, a: 8 }, { q: 2, a: 9 } ] }
   ]
},
{
   _id: 2,
   results: [
      { item: "C", score: 8, answers: [ { q: 1, a: 8 }, { q: 2, a: 7 } ] },
      { item: "B", score: 4, answers: [ { q: 1, a: 0 }, { q: 2, a: 8 } ] }
   ]
}])

I want to remove values from answers where a is 8. my query is

db.collection.update({"results.item":{$in:["C","B"]}},{$pull:{"results.$.answers":{a:8}}})

This query is working fine but updating only one document. Please help.

my desired output is

([{
   _id: 1,
   results: [
      { item: "A", score: 5, answers: [ { q: 1, a: 4 }, { q: 2, a: 6 } ] },
      { item: "B", score: 8, answers: [ { q: 2, a: 9 } ] }
   ]
},
{
   _id: 2,
   results: [
      { item: "C", score: 8, answers: [ { q: 2, a: 7 } ] },
      { item: "B", score: 4, answers: [ { q: 1, a: 0 } ] }
   ]
}])

Thanks in advance.

1
  • 2
    @Sam That's wrong. So please don't encourage the wrong answer either. Commented Mar 4, 2019 at 13:20

2 Answers 2

1

You need filtered positional $[<identifier>] support from MongoDB 3.6 or greater for a single statement:

db.collection.updateMany(
  { "results.item": { "$in": ["C","B"] } },
  { "$pull": { "results.$[el].answers": { "a": 8 } } },
  { "arrayFilters":  [{ "el.item": { "$in": ["C", "B"] } }] }
)

Results in:

{
        "_id" : 1,
        "results" : [
                {
                        "item" : "A",
                        "score" : 5,
                        "answers" : [
                                {
                                        "q" : 1,
                                        "a" : 4
                                },
                                {
                                        "q" : 2,
                                        "a" : 6
                                }
                        ]
                },
                {
                        "item" : "B",
                        "score" : 8,
                        "answers" : [
                                {
                                        "q" : 2,
                                        "a" : 9
                                }
                        ]
                }
        ]
}
{
        "_id" : 2,
        "results" : [
                {
                        "item" : "C",
                        "score" : 8,
                        "answers" : [
                                {
                                        "q" : 2,
                                        "a" : 7
                                }
                        ]
                },
                {
                        "item" : "B",
                        "score" : 4,
                        "answers" : [
                                {
                                        "q" : 1,
                                        "a" : 0
                                }
                        ]
                }
        ]
}
Sign up to request clarification or add additional context in comments.

3 Comments

@Thamaraiselvam Because "B" is in two documents. That's the only reason.
Ahh...🤷‍♂️🤷‍♂️🤷‍♂️🤷‍♂️🤷‍♂️🤷‍♂️
Thank you Neil Lunn for the right solution This solution works perfect as expected on MongoDB version 3.6 and above but My application is currently running on MongoDB 3.4. Is their any solution which can work on 3.4 version?
0

By default, the update() method updates a single document. Set the Multi Parameter to update all documents that match the query criteria.

set {multi: true} or Use updateMany

Reference: https://docs.mongodb.com/manual/reference/method/db.collection.update/

3 Comments

Thanks for replying very soon. I used this db.collection.update({"results.item":{$in:["C","B"]}},{$pull:{"results.$.answers":{a:8}}}, {multi:true}). this is working but from one document it is removing only one value for example from _id: 2 document it should remove one object from item "C" and one from item "B" but by this query it removes only from item "C"
That's not the part that does this. Updating "multiple array elements" is different to "multiple documents".
That's why I answered.

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.