1

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'}}}})

1 Answer 1

2

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")
        }
    ]
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank You Disposer, it worked. I do not have much experience with MongoDB. The concept is kind of hard for me to grasp but i will get better hopefully.

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.