I'm completely new to Java. I'm writing an API to update user customer mapping details in MongoDB
The requirement is to update a record in Mongo, if it is already existing.
If the record is not there then we need to insert new record.
The Payload of API is something similar.
{
userid : "ABC",
customers :{
cmpId : "customer1",
cmpid : "customer2"
}
}
The record in Mongo is similar to the above structure.
If we are entering the details for "ABC" for the first time, a record should be inserted. If a record with userid exists , then we have to loop through the customers and add those which are not there.
So I have written something like this in the service layer :
public CustomerUserMappingDTO AddData(CustomerUserMappingDTO body)
{
Mono<CustomerUserMappingDTO> existingUser = customerRepository.getUserId(body.getUserid());
if () // how to check if the record exists
{
// logic to update data in MongoDB
}else {
// logic to insert data to MongoDB
}
}
I have read something about flapMap to check the tupils , but I'm unable to figure out how to check it.
Can anyone help me here. Many thanks.