如何在mongoDB中删除数组中的数组?

时间:2015-05-11 12:31:21

标签: arrays mongodb

我需要移除一个位于另一个内的数组,只找到删除数组元素的查询(没有他在另一个内部)。

如何删除a数组中的特定元素(ID)是什么?我的查询出了什么问题我得到错误?

有更好的方法来解决这个问题吗?不是数组中的数组

文档

{
    "_id" : "8d127e9ca7178f900dc4953725188da9",
    "dataCriacao" : "2015-05-11T09:10:56.251-0300",
    "pecas" : [ 
        {
            "id" : "49919187a3c4871923dcda506eef5f73",
            "path" : null,
            "nome" : "Whirlpool_LinhaBranca_Positivo_CanaisdeAfinidade_SuperBanner_-.swf",
            "pathOriginal" : "1401 Pecas Midia Grafica_v2/Whirlpool_LinhaBranca_Positivo_CanaisdeAfinidade_SuperBanner_-.swf",
            "imagem" : null,
            "comentarios" : [ 
                {
                    "id" : "c1c586be8ded3d044f96fccc18473cf8",
                    "comentario" : "teste comentario",
                    "horario" : "2015-05-11T09:11:55.399-0300"
                }, 
                {
                    "id" : "dfc10dc2029ad93cf9585a3ecb3ebe51",
                    "comentario" : "teste comentario",
                    "horario" : "2015-05-11T09:11:55.418-0300"
                }, 
                {
                    "id" : "3ef39b18f6d7dc45d6d6f7ccb27a2e10",
                    "comentario" : "teste comentario2",
                    "horario" : "2015-05-11T09:12:11.124-0300"
                }, 
                {
                    "id" : "72b88be895da0b9b7f66c7be55d3ae3b",
                    "comentario" : "teste comentario2",
                    "horario" : "2015-05-11T09:12:11.140-0300"
                }
            ]
        }, 
        {
            "id" : "b22b488ed7302995fa2971d69f425516",
            "path" : null,
            "nome" : "Whirlpool_LinhaBranca_Positivo_DeskMedia_Widget_-.jpg",
            "pathOriginal" : "1401 Pecas Midia Grafica_v2/Whirlpool_LinhaBranca_Positivo_DeskMedia_Widget_-.jpg",
            "imagem" : null
        }, 
        {
            "id" : "846bf4984f533b46de83b0fea1b1e06d",
            "path" : null,
            "nome" : "Whirlpool_LinhaBranca_BuscaDescontos_Homepage_Cupom_-.jpg",
            "pathOriginal" : "1401 Pecas Midia Grafica_v2/Whirlpool_LinhaBranca_BuscaDescontos_Homepage_Cupom_-.jpg",
            "imagem" : null
        }, 
        {
            "id" : "5574d989005d89f9181ae1d4acffd7b3",
            "path" : null,
            "nome" : "Whirlpool_LinhaBranca_BuscaDescontos_Homepage_SuperBanner_-.swf",
            "pathOriginal" : "1401 Pecas Midia Grafica_v2/Whirlpool_LinhaBranca_BuscaDescontos_Homepage_SuperBanner_-.swf",
            "imagem" : null
        }, 
        {
            "id" : "3cddcc834c466e945fe5a97ecb75cbf3",
            "path" : null,
            "nome" : "Whirlpool_LinhaBranca_Positivo_DeskMedia_Widget.jpg",
            "pathOriginal" : "1401 Pecas Midia Grafica_v2/Whirlpool_LinhaBranca_Positivo_DeskMedia_Widget.jpg",
            "imagem" : null
        }, 
        {
            "id" : "cebfef8ed985551560cfd72ac72535dd",
            "path" : null,
            "nome" : "Whirlpool_LinhaBranca_Zoom_Newsletter_Destaque_-.jpg",
            "pathOriginal" : "1401 Pecas Midia Grafica_v2/Whirlpool_LinhaBranca_Zoom_Newsletter_Destaque_-.jpg",
            "imagem" : null
        }, 
        {
            "id" : "60a64c5154c79379a6c1c6cb1685ead6",
            "path" : null,
            "nome" : "Whirlpool_LinhaBranca_Positivo_CanaisdeAfinidade_Retangulo_-.swf",
            "pathOriginal" : "1401 Pecas Midia Grafica_v2/Whirlpool_LinhaBranca_Positivo_CanaisdeAfinidade_Retangulo_-.swf",
            "imagem" : null
        }
    ],
    "validacaoId" : "55509b6dccf2d4a2f3545fcb",
    "email" : "daniela.morais@sofist.com.br",
    "instancia" : "sofist",
    "tipo" : "3"
}

查询1

db.getCollection('validacao_permalinks').update(
    {_id: "8d127e9ca7178f900dc4953725188da9"}, 
    {
        $pull: { "pecas.comentarios": {
            $elemMatch: {id:"c1c586be8ded3d044f96fccc18473cf8"}
            }
        }
     }
 )

错误

cannot use the part (pecas of pecas.comentarios) to traverse the element

2 个答案:

答案 0 :(得分:6)

这是解决方案试试这个,

db.getCollection('validacao_permalinks').update(
    {'pecas.comentarios.id': "c1c586be8ded3d044f96fccc18473cf8"}, 
    {
        $pull: { 'pecas.$.comentarios': {
            "id":"c1c586be8ded3d044f96fccc18473cf8"}            
        }
     }
 )

{'pecas.comentarios.id': "c1c586be8ded3d044f96fccc18473cf8"},会找到该文档,

然后$pull将从阵列中删除该文档。

以下是对MongoDB删除功能的一些其他参考

http://docs.mongodb.org/manual/reference/operator/update/pull/

答案 1 :(得分:-2)

以下是我的工作示例:

Floors是包含房间数组的集合,此查询删除具有指定ID的“room”对象:

db.floors.update(
   { _id: ObjectId("547dc92e85fca4dc0b0c6f26") },
   {
        $pull: { rooms: {
            _id: ObjectId("547dc94685fca4dc0b0c6f29")
            } }
   },
   {upsert:false }
)
db.floors.find()