从 MongoDB 中的数组中删除完整行

Remove Complete Row From Array in MongoDB

我在 mongoDB collection 中有一个文档,其格式如下所示:

DB collection 称为 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")
        }
    ]
}

我需要删除包含 的整个 object"fileInfo.name = 'roman.STL';

这相当于从数组中删除一行。

我尝试了很多 update 组合,但其中 none 似乎有效。

有谁知道哪里出了问题吗?

以下是我尝试过的查询:

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

试试这个:

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

结果将是:

{
    "_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")
        }
    ]
}