1

My Document structure is

"MainAccounts" : [ 
    {
        "orgs" : "5808ba773fe315441b9e0a9e",
        "_id" : ObjectId("5808bc0c3fe315441b9e0b1a"),
        "accounts" : [ 
            "5808baf33fe315441b9e0aa7", 
            "5808baf33fe315441b9e0aa8", 
            "5808baf33fe315441b9e0aa9", 
            "5808baf33fe315441b9e0aa1"
        ]
    },
     {
        "orgs" : "5808ba773fe315441b9e0a9f",
        "_id" : ObjectId("5808bc0c3fe315441b9e0b1b"),
        "accounts" : [ 
            "5808baf33f35425852s255s7", 
            "5808baf3sd23s2d3d4w5s2s8", 
            "5808baf33sd211ds2d2sdsa9", 
            "5808baf33dssd2d21b9e0aa1"
        ]
    }
], 

I want to pull out a particular account say "5808baf33fe315441b9e0aa8" from this, i wrote the query like this.

{ $pull: { "MainAccounts.$.accounts": "5808baf33fe315441b9e0aa8"} }

It gives only error as "The positional operator did not find the match needed from the query. Unexpanded update: MainAccounts.$.accounts"

 { $pull: { "MainAccounts.0.accounts": "5808baf33fe315441b9e0aa8" } }

If i give like this it will remove that value only which gives the expected output.

i need output as

 "MainAccounts" : [ 
    {
        "orgs" : "5808ba773fe315441b9e0a9e",
        "_id" : ObjectId("5808bc0c3fe315441b9e0b1a"),
        "accounts" : [ 
            "5808baf33fe315441b9e0aa7",                 
            "5808baf33fe315441b9e0aa9", 
            "5808baf33fe315441b9e0aa1"
        ]
    },
     {
        "orgs" : "5808ba773fe315441b9e0a9f",
        "_id" : ObjectId("5808bc0c3fe315441b9e0b1b"),
        "accounts" : [ 
            "5808baf33f35425852s255s7", 
            "5808baf3sd23s2d3d4w5s2s8", 
            "5808baf33sd211ds2d2sdsa9", 
            "5808baf33dssd2d21b9e0aa1"
        ]
    }
], 

here i am not able to delete value from second array i need to give

 { $pull: { "MainAccounts.1.accounts": "5808baf33fe315441b9e0aa8" } }

But i need to loop through, any help is appreciated.

4
  • 1
    What is someId? The positional $ operator usage is correct, but the requirement for that to work is actually matching the element in the "query" part, which is what should be in someId. What is MainAccounts? is that actually part of the collection or part of your document? If part of the document, then we have other problems here. Commented May 24, 2017 at 5:17
  • try this one : collection.update(someId, { $pull: { "MainAccounts.accounts": req.params.accountId } }) Commented May 24, 2017 at 6:06
  • @Neil Lunn Please leave all the things "someId". kindly consider my $pull -query. how can i pull out a particular element from that nested array- { $pull : {"MainAccounts.$.accounts" : "5808baf33fe315441b9e0aa8" } } Commented May 24, 2017 at 8:38
  • Refer to stackoverflow.com/questions/5228210/… Commented Jul 1, 2019 at 10:07

3 Answers 3

1

You will get an error: "Cannot apply $pull to a non-array value"

This should be :

db.collection.update({'MainAccounts.accounts': '5808baf33fe315441b9e0aa8'}, {$pull: {MainAccounts:{ accounts: '5808baf33fe315441b9e0aa8'}}})

Here is a reference to this:

mongodb Cannot apply $pull/$pullAll modifier to non-array, How to remove array element

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

1 Comment

The above command will remove the whole nested json(orgs, _id, accounts) not just 5808baf33fe315441b9e0aa8
-1

db.collection.update({someId,{$pull : {"MainAccounts":{"accounts":"5808baf33fe315441b9e0aa8"}}}})

someId could be your _id. Remember if you have to access the document inside the array you cant access it without . operator only.You have to use the index with it.The other way mongodb can access it is by the use of braces.

1 Comment

Hi This one also removes all the accounts array elements instead of only 5808baf33fe315441b9e0aa8.
-1

This will do what you want:

db.collection.update({'MainAccounts.accounts': '5808baf33fe315441b9e0aa8'}, {$pull: {'MainAccounts.$.accounts': '5808baf33fe315441b9e0aa8'}})

4 Comments

This also does the same, account array consisting of 5808baf33fe315441b9e0aa8 is getting removed
I don't understand what you are trying to do. You wrote that removing that value only gives the expected output and the above code will do that.
Hi andrew, this code gives "MainAccounts" : [ { "orgs" : "5808ba773fe315441b9e0a9e", "_id" : ObjectId("5808bc0c3fe315441b9e0b1a"), "accounts" : [ ] }, but what i need is to remove only that particular value "MainAccounts" : [ { "orgs" : "5808ba773fe315441b9e0a9e", "_id" : ObjectId("5808bc0c3fe315441b9e0b1a"), "accounts" : [ "5808baf33fe315441b9e0aa7", "5808baf33fe315441b9e0aa9", "5808baf33fe315441b9e0aa1" ] },} , this is what i need
See my answer. I got a "Cannot apply $pull to a non-array value" when doing this one.

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.