MongoDb: How to unset attribute from nested arrays?

mongodb

Solution

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.

Problem

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!

Original source