I have already know to update objects in a List. I use $ operator to
update objects in a List.
But, this time i have a data structure which got some more level.
Its looks like,
@Entity
public class LevelOne{
@Id
ObjectId id;
@Embedded
List<LevelTwo> nextLevel;
}
public class LevelTwo{
ObjectId id;
List<LevelThree> nextLevel;
}
public class LevelThree{
ObjectId id;
List<LevelFour> nextLevel;
}
public class LeveFour{
ObjectId id;
String someData;
}
Its like that, in the above data structure,
How could i update an object in the lists of class "LevelTwo" &
"LevelThree"?
I have tried the below things.
//This works, when i try to update the LevelTwo Object in the
"LevelOne" class list.
Query<LevelOne> query =
ds.createQuery(LevelOne.class).field(Mapper.ID_KEY).equal(new
ObjectId("4e874bb8b9cf12f274e093b7"))
.filter("levelList.id", new ObjectId("4e874bb8b9cf12f273e093b7"));
UpdateOperations<LevelOne> ops =
ds.createUpdateOperations(LevelOne.class).disableValidation().set("levelList.
$.levelNumber", 3333333);
ds.update(query, ops);
//When i try to update the LevelThree Objects in the "LevelTwo" class
list, this fails.
Query<LevelOne> query =
ds.createQuery(LevelOne.class).field(Mapper.ID_KEY).equal(new
ObjectId("4e874bb8b9cf12f274e093b7"))
.filter("levelList.id", new
ObjectId("4e874bb8b9cf12f273e093b7")).filter("
levelList.levelList.id",
new ObjectId("4e874bb8b9cf12f271e093b7"));
UpdateOperations<LevelOne> ops =
ds.createUpdateOperations(LevelOne.class).disableValidation().set("levelList.levelList.
$.levelNumber", 3333333);
ds.update(query, ops);
/**********
I tried this version of UpdateOperations too,
UpdateOperations<LevelOne> ops =
ds.createUpdateOperations(LevelOne.class).disableValidation().set("levelList.
$.levelList.$.levelNumber", 3333333);
ds.update(query, ops);
*****************/
In both the case, they fails and i get the below exceptions,
com.mongodb.MongoException: can't append to array using string field
name [levelList]
at com.mongodb.DBTCPConnector._checkWriteError(DBTCPConnector.java:
136)
at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:157)
at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:141)
at com.mongodb.DBApiLayer$MyCollection.update(DBApiLayer.java:317)
at com.google.code.morphia.DatastoreImpl.update(DatastoreImpl.java:
950)
at com.google.code.morphia.DatastoreImpl.update(DatastoreImpl.java:
916)
at com.google.code.morphia.DatastoreImpl.update(DatastoreImpl.java:
921)
at com.google.code.morphia.DatastoreImpl.update(DatastoreImpl.java:
837)
at MorphiaNestedUpdateMain.updateNested(MorphiaNestedUpdateMain.java:
66)
at MorphiaNestedUpdateMain.main(MorphiaNestedUpdateMain.java:34)
I guess i am messing with the Syntax.
Any suggestion would be appreciative.
Thanks
-Tamil Vendhan