I have a collection called "XX" and in this collection I have several documents. I am matching on the field "userEmail".
This is the structure of the document:
{ "_id" : { "$oid" : "5331e4e313163623bb649249"} ,
"userEmail" : "[email protected]"}
"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"
}]
}
And now I need find the document where "userEmail" = "[email protected]" and update the fields of the array where "idRest" = 24. in this case:
{
"idRest" : "24" ,
"userPhone" : "88888888" ,
"date" : "03-13-2015" ,
"hour" : "17:30"
},
Can you help me please to build the query using JAVA driver :) thanks in advance!!
EDIT: this is my code, but instead of update the fields add a new sub document
`
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);`