Say I have the following Collections
public @Data class Customer {
@Id
private String id;
private String firstName;
private String lastName;
@DBRef
private List<Address> addressList= new ArrayList<Address>();
}
and
public @Data class Address {
@Id
private String id;
private String address;
private String type;
private String customerID;
}
And each Customer has multiple addresses, and I have implemented MongoRepository
. Saving customer for the First time is working pretty well customerRepo.save(customerObject)
and before calling the save I am persisting multiple Address Objects and then setting those to the addressList
.
Next time when I am updating the same document and want to add a New set of Address to the existing list it is overwriting the whole addressList
array. So basically what I have to do now to set new address like thisexistingCustomerObject.getAddressList().addAll(my new List of address)
if there are thousand(or more than thousand) of elements or I am slicing the addressList array the following procedure won't be a good idea. My question is what is the best way to achieve this scenario? say if I don't want to use MongoTemplate
. Is it possible Just using the MongoRepository