Andradez Krisz
Andradez Krisz

Reputation: 13

Remove Complete Row From Array in MongoDB

I have a document in mongoDB collection and it is formatted as shown below:

The DB collection is called users.

{
    "_id" : ObjectId("54be94e4715beaf4195b89e4"),
    "files" : [ 
        {
            "theName" : "Celtic Statue",
            "fileInfo" : { "size" : 278584,"name" : "celtic.STL" },
            "tags" : [ "statue", "celtic"],
            "updated" : ISODate("2015-01-20T18:15:19.071Z")
        }, 
        {
            "theName" : "Hindu Statue",
            "_id" : ObjectId("54be9b3629e2ae44248c0cfa"),
            "fileInfo" : {"size" : 278584, "name" : "hindu.STL"
            },
            "tags" : [ "decoration",  "hindu"],
            "updated" : ISODate("2015-01-20T18:15:17.071Z")
        }, 
        {
            "theName" : "roman.STL",
            "_id" : ObjectId("54be9b3629e2ae44248c0cfb"),
            "fileInfo" : {"size" : 278584, "name" : "roman.STL"},
            "tags" : [  "statue",  "roman"],
            "updated" : ISODate("2015-01-20T18:15:18.071Z")
        }
    ]
}

I need to remove the whole object that contains "fileInfo.name = 'roman.STL';

This would be the equivalent as removing a row from an array.

I have tried many update combinations but none of them seems to work.

Does anyone know what is wrong?

Below are the queries i have tried:

1- db.users.findAndModify({query:{"_id":ObjectId("54be9b3629e2ae44248c0cfa")}, sort :{}, update :{$pull : {'files' : { 'fileInfo' : { 'name' : 'roman.STL' } }}}})

2- db.users.update({_id :ObjectId("54be9b3629e2ae44248c0cfa")},{$pull : {'files' : { 'fileInfo' : { 'name' : 'roman.STL'}}}})

3-  db.users.update({"_id" : ObjectId("54be9b3629e2ae44248c0cfa")}, {$pull : {files : { fileInfo :  { $elemMatch : { name : 'roman.STL' } }}}});

4- db.users.update({_id :ObjectId("54be9b3629e2ae44248c0cfa"), 'files.fileInfo.name' : 'roman.STL'},{$pull : {'files' : { 'fileInfo' : { 'name' : 'roman.STL'}}}})

Upvotes: 1

Views: 39

Answers (1)

Disposer
Disposer

Reputation: 6371

Try this:

db.foo.update(
   {_id :ObjectId("54be94e4715beaf4195b89e4")},
   { $pull : { 'files' : { 'fileInfo.name' : 'roman.STL'}}}
)

the result will be:

{
    "_id" : ObjectId("54be94e4715beaf4195b89e4"),
    "files" : [ 
        {
            "theName" : "Celtic Statue",
            "fileInfo" : {
                "size" : 278584,
                "name" : "celtic.STL"
            },
            "tags" : [ 
                "statue", 
                "celtic"
            ],
            "updated" : ISODate("2015-01-20T18:15:19.071Z")
        }, 
        {
            "theName" : "Hindu Statue",
            "_id" : ObjectId("54be9b3629e2ae44248c0cfa"),
            "fileInfo" : {
                "size" : 278584,
                "name" : "hindu.STL"
            },
            "tags" : [ 
                "decoration", 
                "hindu"
            ],
            "updated" : ISODate("2015-01-20T18:15:17.071Z")
        }
    ]
}

Upvotes: 2

Related Questions