任何人都可以帮我创建mongo查询以删除" name" :" gdfgdfgdfg"嵌入式文件
对象存储在Mongo db。
中{
"_id": ObjectId("50656f33a4e82d3f98291eff"),
"description": "gdfgdfgdfg",
"menus": [
{
"name": "gdfgdfgdfg"**,
"description": "dfgdgd",
"text": "dfgdfg",
"key": "2",
"onSelect": "yyy",
"_id": ObjectId("50656f3ca4e82d3f98291f00")
},
{
"name": "dfg",
"description": "dfgdfgdfgdf",
"text": "dfgdgf",
"key": "1",
"onSelect": "uuuu",
"_id": ObjectId("50656f44a4e82d3f98291f01")
}
]
}
任何人都帮助我,我是Mongo的新手
答案 0 :(得分:1)
在JavaScript shell中,您可以这样做:
var query = {"_id": ObjectId("50656f33a4e82d3f98291eff")};
db.collection.update(query, {'$pull':{ menus: {name : 'gdfgdfgdfg'} } });
或使用Id。
db.collection.update(query, {'$pull': { menus: {"_id": ObjectId("50656f3ca4e82d3f98291f00")} } });
使用Java Driver应该是这样的:
BasicDBObject query = new BasicDBObject("_id", new ObjectId("50656f33a4e82d3f98291eff"));
BasicDBObject docToRemove = new BasicDBObject("name", "gdfgdfgdfg");
BasicDBObject updateCommand = new BasicDBObject("$pull", new BasicDBObject("menus", docToRemove));
collection.update(query, updateCommand);
答案 1 :(得分:0)
Mongo不会让您删除嵌入的文档。您需要做的是从集合中获取对象,删除列表中的一个对象,然后将其保存回数据库。
obj = db.collection.findOne({"_id": ObjectId("50656f33a4e82d3f98291eff")});
menus = obj.menus.splice(0,1); // Or some other way to manually delete
// the one item in the list
db.collection.update({"_id": ObjectId("50656f33a4e82d3f98291eff")},
{$set: {menus: menus}});
这很复杂,请参阅here。
编辑:如果您不知道索引,可以试试这个:
obj = db.collection.findOne({"_id": ObjectId("50656f33a4e82d3f98291eff")});
var i = 0;
for(i=0;i<obj.menus.length;i++) {
if(obj.menus[i].name === "gdfgdfgdfg")
break;
}
menus = obj.menus.splice(i,1);
db.collection.update({"_id": ObjectId("50656f33a4e82d3f98291eff")},
{$set: {menus: menus}});
答案 2 :(得分:-1)
$conditionArray=array("_id"=>ObjectId("50656f33a4e82d3f98291eff"));
$dataArray=array("description"=>"");
$db->$collectionName->update($conditionArray,array('$unset' =>$dataArray))->limit(1);