删除嵌套在MongoDB中的数组中的子文档 [英] Remove a subdocument nested in an array in MongoDB

查看:177
本文介绍了删除嵌套在MongoDB中的数组中的子文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要删除此内容:

{
"val" : NumberLong(200),
"chId" : 2,
"reqSys" : "222220005031",
"old" : NumberLong(223),
"isRb" : false
},

来自:

{
"_id" : ObjectId("52d7c25480f0a83293adbbbc"),
"d" : 2014001,
"m" : 123456789,
"topups" : {
    "data" : [
            {
                    "val" : NumberLong(200),
                    "chId" : 2,
                    "reqSys" : "222220005031",
                    "old" : NumberLong(223),
                    "isRb" : false
            },
            {
                    "val" : NumberLong(150),
                    "chId" : 2,
                    "reqSys" : "222220005031",
                    "old" : NumberLong(166),
                    "isRb" : false
            }
    ],
    "total" : {
            "cnt" : 2,
            "revenue" : NumberLong(3500000)
    }
}

我想通过查询{d:2014001, m:123456789}找到对象,并在一个命令中删除具有"val":200的数据数组中的整个对象.但是,如果没有,那么多个命令也可以为我工作.已经尝试过$ pull和$ pullAll,但是我缺少了一些东西.

I would like to find the object by querying {d:2014001, m:123456789} and remove the whole object in the data array that has "val":200 if that is possible in one command. But if not, multiple commands work for me also. Have tried with $pull and $pullAll but I am missing something.

推荐答案

要删除子文档,请使用

To remove sub document use $pull

此查询将从嵌套子文档中删除

This query will remove from nested sub document

 db.collection.update({ d : 2014001 , m :123456789},
                      {$pull : { "topups.data" : {"val":NumberLong(200)} } } )

这篇关于删除嵌套在MongoDB中的数组中的子文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆