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.

arunraj770
  • 767
  • 1
  • 10
  • 29
  • 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. – Neil Lunn May 24 '17 at 05:17
  • try this one : collection.update(someId, { $pull: { "MainAccounts.accounts": req.params.accountId } }) – Dinesh undefined May 24 '17 at 06: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" } } – arunraj770 May 24 '17 at 08:38
  • Refer to https://stackoverflow.com/questions/5228210/how-to-remove-an-element-from-a-doubly-nested-array-in-a-mongodb-document – Pranoy Gn Jul 01 '19 at 10:07

3 Answers3

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

Matt Kuhns
  • 1,328
  • 1
  • 13
  • 26
  • The above command will remove the whole nested json(orgs, _id, accounts) not just 5808baf33fe315441b9e0aa8 – Pranoy Gn Jul 01 '19 at 10:00
-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.

aaditya
  • 555
  • 7
  • 19
-1

This will do what you want:

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

Andrew Winterbotham
  • 1,000
  • 7
  • 13
  • This also does the same, account array consisting of `5808baf33fe315441b9e0aa8` is getting removed – arunraj770 May 26 '17 at 04:05
  • 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. – Andrew Winterbotham May 26 '17 at 19:09
  • 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 – arunraj770 May 29 '17 at 04:43
  • See my answer. I got a "Cannot apply $pull to a non-array value" when doing this one. – Matt Kuhns Aug 30 '18 at 15:28