This is my database structure
{
"_id" : ObjectId("58243c430386650d78d12d53"),
"email" : "[email protected]",
"dentistName" : "Suzuka",
"tempDetails" : [
{
"tempName" : "Elsa",
"city" : "bangalore",
"tempEmail": "[email protected]",
"experience" : "5",
"hiredDates" : [
{
"status" : "Accepted",
"bookingId" : "36Y0YM",
}
],
},
{
"tempName" : "Elsa",
"city" : "bangalore",
"tempEmail": "[email protected]",
"experience" : "5",
"hiredDates" : [
{
"status" : "Hired",
"bookingId" : "92Qhd7",
}
],
},
{
"tempName" : "Elsa",
"city" : "bangalore",
"experience" : "5",
"tempEmail": "[email protected]",
"typeOfPractice" : "Oral Surgery",
"hiredDates" : [
{
"status" : "Hired",
"bookingId" : "95kTe9",
}
],
}
]
}
I am trying to update the Status based on the bookingId. I am not able to update the value . I have tried the following code in JAVA MONGODB but it does not seem to be updating.
BasicDBObject searchQuery = new BasicDBObject();
List<BasicDBObject> andQuery = new ArrayList<BasicDBObject>();
andQuery.add(new BasicDBObject("email", dentalEmail));
andQuery.add(new BasicDBObject("tempDetails.tempEmail",tempEmail));
searchQuery.put("$and", andQuery);
DBCursor cursor = col.find(searchQuery);
if (cursor.count() != 0) {
while (cursor.hasNext()) {
BasicDBList jobStatusList = (BasicDBList) cursor.next().get("tempDetails");
for(int k=0;k<jobStatusList.size();k++)
{
BasicDBObject tempJobObject = (BasicDBObject) jobStatusList.get(k);
BasicDBList hiredDatesList = (BasicDBList) tempJobObject.get("hiredDates");
for(int i =0; i<hiredDatesList.size();i++)
{
BasicDBObject hiredDatesObject = (BasicDBObject) hiredDatesList.get(i);
String dbBookingId = hiredDatesObject.getString("bookingId");
if(dbBookingId.equalsIgnoreCase(bookingId))
{
BasicDBObject doc = new BasicDBObject("$set",
new BasicDBObject().append("tempDetails."+k+".hiredDates."+i+".status", status));
col.update(searchQuery, doc, false, false);
System.out.println(doc);
}else{
result = false;
}
}
Kindly help me out with this issue as I have been struggling to update the record based on the BookingId. I get the document through a query and iterate over the objects and if the bookingid matches I am updating the record. Kindly help let me know what I should be changing..