sql >> Base de Datos >  >> NoSQL >> MongoDB

Mongo actualización de subdocs

Aplanar una matriz podría generar documentos bastante grandes, porque para cada elemento interno de la matriz se deben repetir todos los datos de su elemento externo (y esto para todos los niveles).

Entonces, si aplanar no es una opción, aquí hay una solución mientras espera la funcionalidad mejorada en el Jira mencionado (SERVIDOR-831 ):

  • leer el documento en una variable
  • manipular la matriz
  • actualice el documento, reescribiendo toda la matriz

Teniendo en cuenta sus ejemplos, esto se vería así:

doc = db.xx.findOne( {_id:1} );
doc.properties.forEach( function(p) {
    if ( p.property_id == 2 ) { 
        p.tags.forEach( function(t) {
           if ( t.tag_id == 3 ) {
               t.tag_value = 100;
           }
           else if ( t.tag_id == 4 ) {
               newChannel = {};
               newChannel.channel_id = 5;
               newChannel.channel_name = "test5";
               t.channels.push(newChannel);
           }
        })
    }
});
db.xx.update({_id:1},{$set:{properties:doc.properties}});

El resultado es:

doc = db.xx.findOne({_id:1})
{
    "_id" : 1,
    "properties" : [
        {
            "property_id" : 1,
            "tags" : [
                {
                    "tag_id" : 1,
                    "tag_value" : 1000,
                    "channels" : [
                        {
                            "channel_id" : 1,
                            "channel_name" : "test1"
                        },
                        {
                            "channel_id" : 2,
                            "channel_name" : "test2"
                        }
                    ]
                },
                {
                    "tag_id" : 2,
                    "tag_value" : 2500,
                    "channels" : [
                        {
                            "channel_id" : 2,
                            "channel_name" : "test2"
                        },
                        {
                            "channel_id" : 3,
                            "channel_name" : "test3"
                        }
                    ]
                }
            ]
        },
        {
            "property_id" : 2,
            "tags" : [
                {
                    "tag_id" : 3,
                    "tag_value" : 100,
                    "channels" : [
                        {
                            "channel_id" : 1,
                            "channel_name" : "test1"
                        },
                        {
                            "channel_id" : 3,
                            "channel_name" : "test3"
                        }
                    ]
                },
                {
                    "tag_id" : 4,
                    "tag_value" : 5000,
                    "channels" : [
                        {
                            "channel_id" : 1,
                            "channel_name" : "test1"
                        },
                        {
                            "channel_id" : 5,
                            "channel_name" : "test5"
                        }
                    ]
                }
            ]
        }
    ]
}