4

I need to remove an array that is inside another, found only query's to delete an array element (without him being inside another).

How do I delete a specific element (the ID) of the array what's inside this array? What's wrong with my query I get error?

There is a better way to solve this? Without being an array within an array

Document

{
    "_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" : "[email protected]",
    "instancia" : "sofist",
    "tipo" : "3"
}

Query 1

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

Error

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

2 Answers 2

11

Here Is the solution try this,

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

This {'pecas.comentarios.id': "c1c586be8ded3d044f96fccc18473cf8"}, will find the document,

Then $pull will remove that document form the array.

Here are the some other references to the MongoDB Remove functionality

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

Sign up to request clarification or add additional context in comments.

Comments

-2

Here is my working example:

Floors is collection which contains array of rooms, this query removes "room" object with specified ID:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.