2
{
    "partners" : [
        {
            "partnerId" : 5,
            "topicIds" : [
                "59de26f2e4b0f263704769e5"
            ]
        },
        {
            "partnerId" : 45,
            "topicIds" : [
                "alpha",
                "beta"
            ]
        },
        {
            "partnerId" : 12345,
            "topicIds" : [
                "alpha"
            ]
        }
    ]
}

I want to pull certain topicIds from specified partners. So for example I want to pull alpha topic from partnerId = 45 (the entry should remain with topicIds = ["beta"]).

I tried:

db.mypartnercoll.update({}, {$pull:{"partners":{"partnerId":45, "topicIds":{"$in":["alpha"]}}}})

however this removes the entire partnerId = 45 entry. I will have a single partnerId value and multiple topicIds to pull.

3
  • Try {$pull:{"partners.topicIds":{"partnerId":45, "topicIds":{"$in":["alpha"]}}}} Commented Nov 6, 2017 at 15:05
  • WriteResult({ "nMatched" : 0, "nUpserted" : 0, "nModified" : 0, "writeError" : { "code" : 16837, "errmsg" : "cannot use the part (partners of partners.topicIds) to traverse the element ({partners: [ { partnerId: 5.0, topicIds: [ \"59de26f2e4b0f263704769e5\" ] }, { partnerId: 45.0, topicIds: [ \"alpha\", \"beta\" ] }, { partnerId: 12345.0, topicIds: [ \"alpha\" ] } ]})" } }) Commented Nov 6, 2017 at 15:20
  • Check this: stackoverflow.com/questions/5228210/… Specially, the last answer Commented Nov 6, 2017 at 15:23

1 Answer 1

0

instead of

db.mypartnercoll.update({}, {$pull:{"partners":{"partnerId":45, "topicIds":{"$in":["alpha"]}}}})

use

db.mypartnercoll.update({"partners.$.partnerId":"yourdesired_partnerId"}, 
{$pull:{"partners":{"partnerId":45, "topicIds":{"$in":["alpha"]}}}})

this is an update query so you can specify in the first {} a certain document or array element to operate on

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

Comments

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.