How to remove a specific element from ArrayList in morphia?

1.3k views Asked by At

I am using inherited @Embedded reference of ArrayList in Morphia Entity class.

@Entity
public class First {
  @Embedded private List<Second> secondClass;
  private String title;
  private Long id;
  ...  getter and setter .. methods
}

@Embedded
public class Second {
  @Embedded private List<Third> thirdClass;
  private String titleSecond;
  ...  getter and setter .. methods
}

@Embedded
public class Third {
  private String titleThird;
  private String logoUrl
  ...  getter and setter .. methods
}

Json

{
  "secondClass": [{
    "thirdClass": [{
      "titleThird": "Java",
      "logoUrl": "http://www.artsfon.com/pic/201510//artsfon.com-72885.jpg",
    }, {
      "titleThird": "ios",
      "logoUrl": "http://www.artsfon.com/pic//1920x1080/artsfon.com-72885.jpg",
    }],
    "titleSecond": "Developer"
  }],
  "title": "Software Developer",
  "id" : 1234567890
}

Questions

1) How to remove the element with the given value of titleSecond and id?

2) How to remove the element with the given value of titleThird and id?

For first I tried the following implementation:

UpdateOperations<First> ops;
Query<First> updateQuery = datastore.createQuery(First.class).filter("id", Long.parseLong(id.trim()));

ops = datastore.createUpdateOperations(First.class).disableValidation().removeAll("secondClass.titleSecond", "Developer");
datastore.update(updateQuery, ops);

but it's throwing mapping error:

Write failed with error code 16837 and error message 'cannot use the part (secondClass of secondClass.titleSecond) to traverse the element.

How can I perform the action for question 1 and 2? Any help would be appreciable.

possibly linked question: In Morphia how can i update one embedded Object inside an ArrayList

1

There are 1 answers

3
s7vr On BEST ANSWER

Created records like this.

db.myCollection.insertMany([{
        "_id" : ObjectId("58609ed483ce6037ec33fc8c"),
        "secondClass" : [
                {
                        "thirdClass" : [
                                {
                                        "titleThird" : "Java",
                                        "logoUrl" : "http://www.artsfon.com/pic"
                                },
                                {
                                        "titleThird" : "ios",
                                        "logoUrl" : "http://www.artsfon.com/pic"
                                }
                        ],
                        "titleSecond" : "Developer"
                }
        ],
        "title" : "Software Developer"
}])

My First class. Rest of the classes are same.

@Entity("myCollection")
public class First {

    @Id
    private ObjectId id;
    private String title;
    @Embedded
    private List<Second> secondClass;

Answer to the first part.

UpdateOperations<First> ops;
Query<First> updateQuery = datastore.createQuery(First.class).field("_id").equal(new ObjectId("58609ed483ce6037ec33fc8c"));

Second second = new Second();
second.setTitleSecond("Developer");
ops = datastore.createUpdateOperations(First.class).disableValidation().removeAll("secondClass", second);
datastore.update(updateQuery, ops);

Alternate solution for first part.

UpdateOperations<First> ops;
Query<First> updateQuery = datastore.createQuery(First.class).field("_id").equal(new ObjectId("58609ed483ce6037ec33fc8c"));

ops = datastore.createUpdateOperations(First.class).disableValidation().removeAll("secondClass", new BasicDBObject("titleSecond", "Developer"));
datastore.update(updateQuery, ops);

Answer to the second part.

UpdateOperations<First> ops;
Query<First> updateQuery = datastore.createQuery(First.class).field("_id").equal(new ObjectId("58609ed483ce6037ec33fc8c")).field("secondClass.thirdClass.titleThird").equal("ios");

ops = datastore.createUpdateOperations(First.class).disableValidation().removeAll("secondClass.$.thirdClass", new BasicDBObject("titleThird", "ios"));
datastore.update(updateQuery, ops);