Updating list inside a List using Morphia?

77 views
Skip to first unread message

MaRaVaN

unread,
Oct 3, 2011, 3:19:24 AM10/3/11
to Morphia
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

MaRaVaN

unread,
Oct 3, 2011, 8:52:18 AM10/3/11
to Morphia
Is this feature exists in MongoDB or Am i trying to do which is not
exists in MongoDB???

Scott Hernandez

unread,
Oct 3, 2011, 9:55:10 AM10/3/11
to mor...@googlegroups.com
Yeah, there is no support for that at the server; The positional
operator ($) can only be used at one level, not multiple levels.

https://jira.mongodb.org/browse/SERVER-831

MaRaVaN

unread,
Oct 4, 2011, 4:25:17 AM10/4/11
to Morphia
I voted up. Thanks scott.
For now, i am changing my schema.
Reply all
Reply to author
Forward
0 new messages