Spring Mongo criteria querying data which are present and not present in database

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

This is what I have so far

 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);
            }
                
            }
            
            }
                
            }

Any help will be much appreciated!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.