这是我的mongo doc:
{
"_id" : ObjectId("51f22f6784aefaa97d7e533e"),
"group_name" : "demo",
"privileges" : [
"User Group",
"Profile Images",
"Advertisement"
],
"users" : [
{
"full_name" : "aap",
"password" : "as",
"status" : "Active",
"user_name" : "as"
},
{
"full_name" : "b",
"user_name" : "b",
"password" : "b",
"status" : "Active"
},
{
"full_name" : "prashant",
"user_name" : "pt123",
"status" : "Active"
}
]
}
我想查找所有包含“group_name”的记录:“demo”删除具有“user_name”的特定记录:“pt123”表单用户数组。 我试过了
db.users.update({"group_name":"demo"}, {$pull:{"users.user_name":"pt123"}})
但没有效果。我缺少哪里? 请帮忙 我的java代码是
BasicDBObject del = new BasicDBObject("group_name", z);
del.put("users.user_name", x);
BasicDBObject delquery=new BasicDBObject("$match",del);
con.remove(delquery);
答案 0 :(得分:2)
以下是您的工作查询:
db.users.update({"group_name":"demo"}, {$pull: {"users":{"user_name":"pt123"}}})
答案 1 :(得分:1)
BasicDBObject sq = new BasicDBObject("group_name", z);
BasicDBObject idoc=new BasicDBObject("user_name",x);
BasicDBObject odoc =new BasicDBObject("users",idoc);
BasicDBObject delq=new BasicDBObject("$pull",odoc);
con.coll.update(sq, delq);