如何构造 basicdbobject 查询以删除深度数组文档?
Posted
技术标签:
【中文标题】如何构造 basicdbobject 查询以删除深度数组文档?【英文标题】:How to construct basicdbobject query to delete deep array documnet? 【发布时间】:2012-10-08 12:38:38 【问题描述】:我有如下的 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("5072b33aa4e8dd3e359b8e "),
【问题讨论】:
你的问题没有意义。请尽量说得更清楚,否则您的问题很有可能被关闭 【参考方案1】: 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);
【讨论】:
以上是关于如何构造 basicdbobject 查询以删除深度数组文档?的主要内容,如果未能解决你的问题,请参考以下文章