Cannot $push document

66 views
Skip to first unread message

stephanos

unread,
Jun 13, 2012, 10:29:25 AM6/13/12
to rogue...@googlegroups.com
I tried to 'push' a document to an array of an existing document:

AppCol
        .where(_._id eqs id)
        .modify(_.team.subfield(_.size) inc 1)
        .modify(_.team.subfield(_.members) push member)
        .updateOne(SAFE)

member is of type 'BsonRecord'. So the code above compiles without a problem but at runtime the following happens:

    json can't serialize type : class com.mycompany.model.TeamMemberDoc
    java.lang.RuntimeException: json can't serialize type : class com.mycompany.model.TeamMemberDoc
at com.mongodb.util.JSON.serialize(JSON.java:261)
at com.mongodb.util.JSON.serialize(JSON.java:141)
at com.mongodb.util.JSON.serialize(JSON.java:141)
at com.mongodb.util.JSON.serialize(JSON.java:58)
at com.mongodb.BasicDBObject.toString(BasicDBObject.java:84)
at com.foursquare.rogue.MongoHelpers$MongoBuilder$.buildModifyString(MongoHelpers.scala:124)
at com.foursquare.rogue.BaseModifyQuery.toString(Query.scala:800)

So basically the member document is handed down to the JSON serialization (from the MongoDB Java client) which of course has no clue what to do with it. 
I think Rogue should call '.asDBObject' before that, if it detects that the argument is a 'BsonRecord'.

What do you think?

Marius Seritan

unread,
Jul 25, 2012, 11:55:32 AM7/25/12
to rogue...@googlegroups.com
I am also hitting a variation of this issue. I am trying to use setTo on an array deeper in the document structure and I get "json can't serialize type" where the type is the class of the array item. The class extends from a BsonRecord, has a meta set, the meta is the companion object and it inherits from BsonMetaRecord.

I am using some similar code in another part of the application and it is working there. I have not yet been able to figure out the difference between the two use cases.

Marius Seritan

unread,
Jul 25, 2012, 12:45:20 PM7/25/12
to rogue...@googlegroups.com
My problem was fixed by eliminating one level of containment. The following structure does not seem to work

{ favorites : {version:1,items:[]}}

Flattening it up one level to the following is doing the trick

{favoritesVersion:1, favoritesItems:[]}

The data change is not optimal but it is something we can leave with.

Incidentally liftweb mongo record also added update(), sometimes in the last 6 months. That code also seems to work better with flat structures, when generating the $set/$unset database json it looks for dirty fields only at the top level.

Thanks for your contribution to the scala ecosystem with the Rogue library!

Marius
Reply all
Reply to author
Forward
0 new messages