Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDb: How to unset attribute from nested arrays?

Tags:

mongodb

I'm trying to remove an attribute from a triple-nested array without success. Here is an example of the data I want to remove:

Controls: [
    {    
        Name: 'ControlNumberOne',
        Submit: {   
            Executes: [
                {
                    Name: 'execute',
                    Type: 0
                },
                {
                    Name: 'anotherExecute',
                    Type: 0
                }
            ]
        }
    },
    {    
        Name: 'ControlNumberTwo',
        Submit: {   
            Executes: [
                {
                    Name: 'anotherFromAnotherControl',
                    Type: 1
                }
            ]
        }
    }

]

I tried the following update queries but none of them worked:

  • db.Page.update('Controls.Submit.Executes.Type': { $exists : true } }, { $unset : { 'Controls.Submit.Executes.Type' : 1 } }, false, true);)

  • db.Page.update('Controls.Submit.Executes.Type': { $exists : true } }, { $unset : { 'Controls.$.Submit.Executes.$.Type' : 1 } }, false, true);)

However, if I execute db.Page.find('Controls.Submit.Executes.Type': { $exists : true } }) it does return all the Executes that still have a Type attribute.

Can this be achieved? Thanks!

like image 838
faloi Avatar asked Mar 07 '13 18:03

faloi


People also ask

How do I remove a field 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 you remove an element from a doubly nested array in a MongoDB document?

To remove an element from a doubly-nested array in MongoDB document, you can use $pull operator. Now field "UserZipCode": "20010" has been removed from a doubly-nested array.

How do I unset a field in MongoDB?

The $unset operator deletes a particular field. Consider the following syntax: { $unset: { <field1>: "", ... } } The specified value in the $unset expression (i.e. "" ) does not impact the operation.

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.


2 Answers

If anyone is still looking for an answer (like me), here it is.

With MongoDB version 3.6+, it is now possible to use the positional operator to update all items in an array, including the deeper nested level. See official documentation here.:

db.getCollection('xxx').update(
   {'Controls.Submit.Executes.Type': { $exists : true },
   { $unset : {'Controls.$[].Submit.Executes.$[].Type' : 1}}
)

This code is tested and works well with MongoDB CLI, RoboMongo and Mongo-Java driver.

like image 71
ersnh Avatar answered Sep 21 '22 00:09

ersnh


Querying and updating of nested arrays is not (yet) supported by MongoDB commands directly, this has to be done on the client side:

  • read the document into a variable
  • manipulate the array
  • update the document, rewriting the entire array

See this issue on Jira: https://jira.mongodb.org/browse/SERVER-831 and this thread on stackoverflow: Mongo update of subdocs

Given your example, this would look like this:

db.xx.find(
    {'Controls.Submit.Executes.Type': { $exists : true } }
).forEach( function(doc) {
    doc.Controls.forEach( function(c) {
        c.Submit.Executes.forEach( function(e) {
            if ( e.Type != undefined ) delete e.Type;        
        });
    });
    db.xx.update({_id: doc._id},{$set:{Controls:doc.Controls}});
});

and the result is:

> db.xx.findOne()
{
    "Controls" : [
        {
            "Name" : "ControlNumberOne",
            "Submit" : {
                "Executes" : [
                    {
                        "Name" : "execute"
                    },
                    {
                        "Name" : "anotherExecute"
                    }
                ]
            }
        },
        {
            "Name" : "ControlNumberTwo",
            "Submit" : {
                "Executes" : [
                    {
                        "Name" : "anotherFromAnotherControl"
                    }
                ]
            }
        }
    ],
    "_id" : ObjectId("5159ff312ee0f7d445b03f32")
}
like image 40
ronasta Avatar answered Sep 21 '22 00:09

ronasta