2013-07-27 200 views
0

嵌套的文件,這是我蒙戈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」的所有記錄:「演示」刪除有「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); 

回答

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); 
2

這裏的工作查詢:

db.users.update({"group_name":"demo"}, {$pull: {"users":{"user_name":"pt123"}}})