Click here to Skip to main content
15,881,669 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to query all data which are present in my database by comparing with while loop data and if this data is available I want to update it and if not I want to insert it as a new record into the database.

The problem is I'm only getting data that is present in the database these missing I'm not able to get them from the fetched result

Any help will be much appreciated!

What I have tried:

while(result.next()) {
           Query query = new Query();
           query.addCriteria(Criteria.where("customerId").is(result.getString(1))
                .andOperator(Criteria.where("orderDate").is(result.getString(2)),
                             Criteria.where("Location").is(baseLocation)));

           var dbData = mongoTemplate.find(query, Customer.class);

           if(dbData == null || dbData.isEmpty()) {

           Customer customer = new Customer();
           customer.setCustomerId(result.getString(1));
           customer.setOrderDate(result.getString(2));
           customer.setOrderWeight(result.getString(3));
           customer.setCustomerCode(result.getString(9));
           customer.setDateAdded(LocalDateTime.now());
           customer.setCustomerLocation(baseLocation);
           customerList.add(customer);

           }else {

           for (Customer customerData: dbData) {

           if(customerData.getOrderDate().equals(result.getString(2)) == true) {
           customerData.setCustomerId(result.getString(1));
           customerData.setOrderDate(result.getString(2));
           customerData.setOrderWeight(result.getString(3));
           customerData.setCustomerCode(result.getString(9));
           customerData.setDateAdded(LocalDateTime.now());
           customerData.setCustomerLocation(baseLocation);
           customerList.add(customerData);

           }else {
           Customer customer = new Customer();
           customer.setCustomerId(result.getString(1));
           customer.setOrderDate(result.getString(2));
           customer.setWeight(result.getString(3));
           customer.setCustomerCode(result.getString(9));
           customer.setDateAdded(LocalDateTime.now());
           customer.setCustomerLocation(basename);
           customerList.add(customer);
           }

           }

           }

           }
Posted
Updated 16-Jan-23 22:42pm
Comments
Richard MacCutchan 16-Jan-23 4:11am    
Without seeing the actual results it is impossible to guess what the problem is. Use the debugger to step through the code to see exactly what happens for each query.
Office Systems 16-Jan-23 4:34am    
for (Customer customerData: dbData) {

if(customerData.getOrderDate().equals(result.getString(2)) == true) {
System.out.println("Data Present In Database");
}else {
System.out.println("Data Absent In Database");
}

It prints only "Data Present In Database"
Andre Oosthuizen 16-Jan-23 4:14am    
Your question is confusing - you are saying you are only getting data that is present.... that is exactly what you want is it not? If it is not present, then add a new record - if it is, then edit the record.
Office Systems 16-Jan-23 4:22am    
May be I should put it a little bit more clear @Andre Oosthuizen my concern is I want to update if the data is present within my database and Insert a new record if it does not exist, now the issue I'm facing is that data present inside database are updated correctly but these absent are not inserted and I can't even get them within the if else statement and at the debugger meaning new records are not fetched by the query
Richard MacCutchan 16-Jan-23 4:42am    
As I said earlier, you need to use the debugger to find out exactly what is returned by the query when the requested record does not exist. We cannot do that for you.

1 solution

You are adding a new client twice, at the start of your code and then again within another if else statement.

I am not familiar with the code used ?-Java/MongoDb -? but the basics is the same

How it works -
1. Search data table for client with ID 1.
2. If client exist, check if new data corresponds with table data,
3. If it does not compare, update the record with the new data,
4. If the client does not exist, add a new record to data table.

while(result.next()) {
if(dbData == null || dbData.isEmpty()) {
Customer customer = new Customer();
} else {
UPDATE RECORD HERE...
}


You then have to run a new query again if you want to see the updated or the new record.
 
Share this answer
 
v2
Comments
Office Systems 17-Jan-23 6:11am    
Thanks so much @Andre Oosthuizen that worked perfect👍
Andre Oosthuizen 17-Jan-23 8:35am    
Only a pleasure!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900