如何使用java驱动程序使用两个“where”条件更新MongoDB中的数组项

时间:2014-03-25 20:58:47

标签: java mongodb mongodb-java nosql

我有一个名为“XX”的集合,在这个集合中我有几个文件。我在“userEmail”字段上匹配。

这是文件的结构:

{ "_id" : { "$oid" : "5331e4e313163623bb649249"} ,
   "userEmail" : "user3@this.com"} 
   "reservations" : [ { 
        "idRest" : "23" , 
        "userPhone" : "88888888" , 
        "date" : "03-13-2015" , 
        "hour" : "14:30" 
   },
   { 
        "idRest" : "24" , 
        "userPhone" : "88888888" , 
        "date" : "03-13-2015" , 
        "hour" : "17:30" 
   },
   { 
        "idRest" : "22" , 
        "userPhone" : "88888888" , 
        "date" : "03-13-2015" , 
        "hour" : "16:30" 
  }]

}

现在我需要找到“userEmail”=“some@some.com”的文档并更新“idRest”= 24的数组字段。在这种情况下:

{ 
    "idRest" : "24" , 
    "userPhone" : "88888888" , 
    "date" : "03-13-2015" , 
    "hour" : "17:30" 
},

请帮助我使用JAVA驱动程序构建查询:)提前感谢!!


编辑:这是我的代码,但不是更新字段,而是添加新的子文档

`

DBCollection collReservationByUser = db.getCollection(usersReservationCollection);

DBObject queryx = new BasicDBObject("userEmail", rest.getEmailUser());

BasicDBObject document1 = new BasicDBObject();

document1.put("idRest", rest.getEmailUser());
document1.put("userPhone", rest.getPhoneUser());
document1.put("date", rest.getFecha());
document1.put("hour", rest.getHora());;

DBObject update1 = new BasicDBObject();
update1.put("$push", new BasicDBObject("reservations", document1));
collReservationByUser.update(queryx, update1, true, true);`

2 个答案:

答案 0 :(得分:2)

在shell中,您可以使用如下命令执行此操作:

db.XX.update(
    // Find the document where userEmail=some@some.com and find the element in the "reservations" array where idRest=24
    {
        "userEmail":"some@some.com", 
        "reservations.idRest":24
    }, 
    // Update the element matched by the find criteria with new values
    {
        $set:{
           "reservations.$.idRest":99, 
           "reservations.$.userPhone":"888888", 
           "reservations.$.date":"03-13-2015", 
           "reservations.$.hour":"17:30"
        }
    }
)

Java等价物将如下所示:

    ...
    DBObject selectQuery = new BasicDBObject("userEmail", rest.getEmailUser());
    selectQuery.append("reservations.idRest", rest.getId());

    BasicDBObject updateFields = new BasicDBObject();
    updateFields.put("reservations.$.idRest", rest.getEmailUser());
    updateFields.put("reservations.$.userPhone", rest.getPhoneUser());
    updateFields.put("reservations.$.date", rest.getFecha());
    updateFields.put("reservations.$.hour", rest.getHora());;

    DBObject updateQuery = new BasicDBObject();
    updateQuery.put("$set", updateFields);
    collReservationByUser.update(selectQuery, updateQuery, true, true); 
    ...

注意:如果"预订"数组有多个元素" idRest" = 24,那么上面的查询将仅更新第一个匹配的元素。使用位置运算符无法更新数组中的多个元素。

答案 1 :(得分:0)

更新没有太大区别,请参阅文档中的.update()方法。但您需要$set运算符:

DBObject queryx = new BasicDBObject("userEmail", "some@some.com");
queryx.put("idRest", 24);


BasicDBObject document1 = new BasicDBObject();

document1.put("idRest", rest.getEmailUser());
document1.put("userPhone", rest.getPhoneUser());
document1.put("date", rest.getFecha());
document1.put("hour", rest.getHora());

DBObject update1 = new BasicDBObject();

update1.put("$set", new BasicDBObject("reservations.$", document1));
collReservationByUser.update(queryx, update1);`

您也不需要所有字段,只需将“userPhone”更改为:

BasicDBObject document1 = new BasicDBObject();
document1.put("reservations.$.userPhone", rest.getPhoneUser());

DBObject update1 = new BasicDBObject();

update1.put("$set", document1);
collReservationByUser.update(queryx, update1);`

这依赖于positional $运算符,以便处理数组的“匹配”索引。