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!
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.
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.
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.
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.
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.
Querying and updating of nested arrays is not (yet) supported by MongoDB commands directly, this has to be done on the client side:
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")
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With