Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove an element from a doubly-nested array in a MongoDB document.

Tags:

mongodb

I have a document structure something along the lines of the following:

{ "_id" : "777", "someKey" : "someValue", "someArray" : [     {         "name" : "name1",         "someNestedArray" : [             {                 "name" : "value"             },             {                 "name" : "delete me"             }         ]     }   ] } 

I want to delete the nested array element with the value "delete me".

I know I can find documents which match this description using nested $elemMatch expressions. What is the query syntax for removing the element in question?

like image 277
rshepherd Avatar asked Mar 08 '11 04:03

rshepherd


People also ask

How do I remove a specific element from an array in MongoDB?

To remove an element, update, and use $pull in MongoDB. The $pull operator removes from an existing array all instances of a value or values that match a specified condition.

How do I change the nested array element in MongoDB?

Update Nested Arrays in Conjunction with $[]The $[<identifier>] filtered positional operator, in conjunction with the $[] all positional operator, can be used to update nested arrays. The following updates the values that are greater than or equal to 8 in the nested grades. questions array if the associated grades.

How do I remove a value from a collection in MongoDB?

To delete all documents in a collection, pass an empty document ( {} ). Optional. To limit the deletion to just one document, set to true . Omit to use the default value of false and delete all documents matching the deletion criteria.


2 Answers

To delete the item in question you're actually going to use an update. More specifically you're going to do an update with the $pull command which will remove the item from the array.

db.temp.update(   { _id : "777" },   {$pull : {"someArray.0.someNestedArray" : {"name":"delete me"}}} ) 

There's a little bit of "magic" happening here. Using .0 indicates that we know that we are modifying the 0th item of someArray. Using {"name":"delete me"} indicates that we know the exact data that we plan to remove.

This process works just fine if you load the data into a client and then perform the update. This process works less well if you want to do "generic" queries that perform these operations.

I think it's easiest to simply recognize that updating arrays of sub-documents generally requires that you have the original in memory at some point.


In response to the first comment below, you can probably help your situation by changing the data structure a little

"someObjects" : {   "name1":  {         "someNestedArray" : [             {                 "name" : "value"             },             {                 "name" : "delete me"             }         ]     } } 

Now you can do {$pull : { "someObjects.name1.someNestedArray" : ...

Here's the problem with your structure. MongoDB does not have very good support for manipulating "sub-arrays". Your structure has an array of objects and those objects contain arrays of more objects.

If you have the following structure, you are going to have a difficult time using things like $pull:

array [   { subarray : array [] },   { subarray : array [] }, ] 

If your structure looks like that and you want to update subarray you have two options:

  1. Change your structure so that you can leverage $pull.
  2. Don't use $pull. Load the entire object into a client and use findAndModify.
like image 60
Gates VP Avatar answered Oct 13 '22 00:10

Gates VP


MongoDB 3.6 added $[] operator that facilitates updates to arrays that contain embedded documents. So the problem can be solved by:

db.test.update(   { _id : "777" },   {$pull : {"someArray.$[].someNestedArray" : {"name":"delete me"}}} ) 
like image 45
Cuong Le Ngoc Avatar answered Oct 13 '22 02:10

Cuong Le Ngoc