0

我有如下的 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”),

4

1 回答 1

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);
于 2012-10-08T13:45:46.157 回答