如何构造basicdbobject查询来删除深层数组文档?

时间:2012-10-08 12:38:39

标签: java mongodb

我有mongo文件如下

  "_id" : ObjectId("5072b33aa4e8dd3e359b8e94"), 
  "menus" : { 
    "5072b8dda4e8dd3e359b8ea8" :
     { 
        "_id" : ObjectId("5072b8dda4e8dd3e359b8ea8"), 
        "description" : "hfghfgh",
         "MenuItems" : 
            [   
                {   
                "name" : "dfgdfg",  
                "description" : "dgdfgd", 
                "_id" : ObjectId("5072b91ba4e8dd3e359b8eaa")
                 },     
                { 
                "name" : "fgdfgdf",     
                "description" : "gdfgg",
                 "_id" : ObjectId("5072bb92a4e8204e51de1084") 
                }
            ]
    }
}

其实我曾尝试删除 menuItems

中的以下对象
    { 
    "name" : "fgdfgdf",     
    "description" : "gdfgg",
    "_id" : ObjectId("5072bb92a4e8204e51de1084") 
    }

我的查询如下,但不起作用。

 BasicDBObject query=new BasicDBObject("_id",objectId("...");

  BasicDBObject document = new     BasicDBObject("menus.5072b8dda4e8dd3e359b8ea8.menuItems.$._id", ObjectId("5072bb92a4e8204e51de1084") );
  BasicDBObject updateOps=new BasicDBObject("$pull", document);

我有 menuItems“_id”的id:ObjectId(“5072bb92a4e8204e51de1084”),菜单id为“_id”:ObjectId(“5072b8dda4e8dd3e359b8ea8”)和顶级文档“_id”的id:ObjectId(“5072b33aa4e8dd3e359b8e94” “),

1 个答案:

答案 0 :(得分:0)

 BasicDBObject query=new BasicDBObject("_id",new ObjectId(".....");

BasicDBObject document = new BasicDBObject("menus.5072b8dda4e8dd3e359b8ea8.menuItems",
new  BasicDBObject("_id":ObjectId("5072bb92a4e8204e51de1084") );

BasicDBObject updateOps=new BasicDBObject("$pull", document);

 db.collection.update(query,updateOps);