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

Cómo eliminar un objeto anidado profundo en mongodb

Lo intenté y parece que lo que estás tratando de hacer debería funcionar correctamente. Verificaría su consulta para ubicar el documento adecuado para actualizar y asegurarme de que encuentre lo que desea.

> db.books.findOne()
{
        "_id" : "1234567890",
        "title" : "Lord Of The Rings",
        "books" : {
                "1234567890" : {
                        "_id" : "123456789890",
                        "title" : "The Two Towers",
                        "page_count" : {
                                "en" : 6000,
                                "de" : 7000
                        }
                },
                "2234567890" : {
                        "_id" : "223456789890",
                        "title" : "The Return Of The King",
                        "page_count" : {
                                "en" : 6000,
                                "de" : 7000
                        }
                }
        }
}
> db.books.update({'_id': "1234567890"}, {$unset: {'books.2234567890.page_count': ""}})
> db.books.findOne()
{
        "_id" : "1234567890",
        "books" : {
                "1234567890" : {
                        "_id" : "123456789890",
                        "title" : "The Two Towers",
                        "page_count" : {
                                "en" : 6000,
                                "de" : 7000
                        }
                },
                "2234567890" : {
                        "_id" : "223456789890",
                        "title" : "The Return Of The King"
                }
        },
        "title" : "Lord Of The Rings"
}
>