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..
Did you checked your database properly ? I am able to run your code perfectly fine without errors and it does update the collection as well.
I just created a method where i am doing the following :
private void performOperation() {
MongoClient mongo = new MongoClient("localhost", 27017);
MongoClient mongoClient = new MongoClient();
DB db = mongoClient.getDB("test");
DBCollection table = db.getCollection("vinay");
logger.info("connected successfully");
BasicDBObject searchQuery = new BasicDBObject();
List<BasicDBObject> andQuery = new ArrayList<BasicDBObject>();
andQuery.add(new BasicDBObject("email", "[email protected]"));
andQuery.add(new BasicDBObject("tempDetails.tempEmail", "[email protected]"));
searchQuery.put("$and", andQuery);
logger.info("executing :"+ searchQuery);
DBCursor cursor = table.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("36Y0YM")) {
BasicDBObject doc = new BasicDBObject("$set", new BasicDBObject()
.append("tempDetails." + k + ".hiredDates." + i + ".status", "New Status"));
table.update(searchQuery, doc, false, false);
logger.info(doc);
} else {
logger.info("no record find");
}
}
}
}
}
}
and after i execute this the status where booking ID is 36Y0YM get updated to - "New Status".
But i noticed one this. the way you wrote the loops... because even after the record is updated it iterates again and prints the message "no record found" in your case it will set the result to false, so if you relying on the result then your whole approach would be wrong.
i dont know what logic you are trying to build but the point is there is nothing wrong in your code which updates records in mongodb if there is anything wrong its the loops your wrote.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With