I am working on a customer data loader, where customers can have multiple addresses. If the customer is not found, I create it and add the address. If the customer exists, I simply add the new address, like this:
DBObject findCustomer = new BasicDBObject();
findCustomer.put("email", custEmail);
//check for existing customer
DBObject newCustomer = customerCollection.findOne(findCustomer);
if (newCustomer == null) {
//INSERT
newCustomer = new BasicDBObject();
newCustomer.put("firstname", firstname);
newCustomer.put("lastname", lastname);
newCustomer.put("email", custEmail);
newCustomer.put("password", custData.getPassword());
newCustomer.put("softwaretime", new Date());
}
DBObject newAddress = new BasicDBObject();
City tempCity = new City();
tempCity = addressData.getCity();
newAddress.put("type", addressData.getType());
newAddress.put("line1", addressData.getLine1());
newAddress.put("line2", addressData.getLine2());
newAddress.put("city", tempCity.getCity());
newAddress.put("state", tempCity.getState());
newAddress.put("postal", tempCity.getZip());
newAddress.put("country", tempCity.getCountry());
newCustomer.put("address", newAddress);
customerCollection.save(newCustomer);
This works for new customers. The problem is that when the customer already exists, the new address overwrites the existing address.
How can I add the new address to the customer, so that it will keep multiple addresses?
From what I've found, I should be able to accomplish this with a "push" via the shell. But I don't see "push" as method on the BasicDBObject.