0

这是我的 mongo 文档:

{
    "_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);
4

2 回答 2

2

这是您的工作查询:

db.users.update({"group_name":"demo"}, {$pull: {"users":{"user_name":"pt123"}}})
于 2013-07-27T10:37:46.393 回答
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);
于 2013-07-27T11:26:43.293 回答