How to update embedded object list in morphia?

1k Views Asked by At

I am new to Morphia and trying to update the existing embedded arrayList of an object. Here is my class:

@Entity
public class Student {
  @Embedded private List<Address> address;
  private String name;
  private Long id;
  ...  getter and setter .. methods
}

@Embedded
public class Address {
  private Long customId;
  private String name;
  ...  getter and setter .. methods
}

Json for the above class:

{
 "student":{
    "address": [{
      "customId": "123456",
      "name": "Jack"
    }, {
      "customId": "78901",
      "name": "sam"
    }],
  "name": "Teacher",
  "id" : 1234567890
}

I have to update the address.name where address.customId is 78901. I tried to follow the Morphia documentation but couldn't find anything.

I thought to remove the elements first with 78901 address.customId then append the data into existing list. For removing the data I did this:

 UpdateOperations<Student> ops;
        Query<Student> updateQuery = datastore.createQuery(Student.class).filter("id", 1234567890);
        ops = datastore.createUpdateOperations(Student.class).disableValidation().removeAll("address", new BasicDBObject("customId", 78901));

Above code is successfully removing the expecting data but I am not sure how to add more data to existing list. Any help would be appreciable. Thanks

0

There are 0 best solutions below