embedded sequences, updating

37 views
Skip to first unread message

Maarten Koopmans

unread,
Oct 17, 2012, 10:47:13 AM10/17/12
to rogue...@googlegroups.com
Hi,

What's the royal road to embedding a sequence with Rogue (a MongoJsonObjectListField in Lift) and more importantly, updating it by adding/removing an item?

(from what I learnt on the Lift ml $push or $pull on a JValue when using Record ~ but how to do that with Rogue in a clean way?)

Thanks!

--Maarten

Jason Liszka

unread,
Oct 17, 2012, 11:03:15 AM10/17/12
to rogue...@googlegroups.com
You can use MongoListField for lists of native types (string, int, etc), or BsonRecordListField for lists of bson/json objects.

Some sample queries from QueryTest.scala (see TestModels.scala for record declarations):

    // Lists
    Venue.where(_.legacyid eqs 1).modify(_.popularity setTo List(5))      .toString() must_== query + """{ "$set" : { "popularity" : [ 5]}}""" + suffix
    Venue.where(_.legacyid eqs 1).modify(_.popularity push 5)             .toString() must_== query + """{ "$push" : { "popularity" : 5}}""" + suffix
    Venue.where(_.legacyid eqs 1).modify(_.tags pushAll List("a", "b"))   .toString() must_== query + """{ "$pushAll" : { "tags" : [ "a" , "b"]}}""" + suffix
    Venue.where(_.legacyid eqs 1).modify(_.tags addToSet "a")             .toString() must_== query + """{ "$addToSet" : { "tags" : "a"}}""" + suffix
    Venue.where(_.legacyid eqs 1).modify(_.popularity addToSet List(1L, 2L)).toString() must_== query + """{ "$addToSet" : { "popularity" : { "$each" : [ 1 , 2]}}}""" + suffix
    Venue.where(_.legacyid eqs 1).modify(_.tags popFirst)                 .toString() must_== query + """{ "$pop" : { "tags" : -1}}""" + suffix
    Venue.where(_.legacyid eqs 1).modify(_.tags popLast)                  .toString() must_== query + """{ "$pop" : { "tags" : 1}}""" + suffix
    Venue.where(_.legacyid eqs 1).modify(_.tags pull "a")                 .toString() must_== query + """{ "$pull" : { "tags" : "a"}}""" + suffix
    Venue.where(_.legacyid eqs 1).modify(_.popularity pullAll List(2L, 3L)) .toString() must_== query + """{ "$pullAll" : { "popularity" : [ 2 , 3]}}""" + suffix
    Venue.where(_.legacyid eqs 1).modify(_.popularity at 0 inc 1)         .toString() must_== query + """{ "$inc" : { "popularity.0" : 1}}""" + suffix
    // alternative syntax
    Venue.where(_.legacyid eqs 1).modify(_.popularity idx 0 inc 1)        .toString() must_== query + """{ "$inc" : { "popularity.0" : 1}}""" + suffix

    // BsonRecordField subfield queries
    Venue.where(_.claims.subfield(_.status) contains ClaimStatus.approved).toString() must_== """db.venues.find({ "claims.status" : "Approved"})"""
    Venue.where(_.lastClaim.subfield(_.userid) eqs 123)              .toString()      must_== """db.venues.find({ "lastClaim.uid" : 123})"""
    Venue.where(_.claims.subfield(_.source.subfield(_.name)) contains "twitter").toString() must_== """db.venues.find({ "claims.source.name" : "twitter"})"""

    // select subfields
    Tip.where(_.legacyid eqs 1).select(_.counts at "foo").toString() must_== """db.tips.find({ "legid" : 1}, { "counts.foo" : 1})"""
    Venue.where(_.legacyid eqs 1).select(_.geolatlng.unsafeField[Double]("lat")).toString() must_== """db.venues.find({ "legid" : 1}, { "latlng.lat" : 1})"""
    Venue.where(_.legacyid eqs 1).select(_.lastClaim.subselect(_.status)).toString() must_== """db.venues.find({ "legid" : 1}, { "lastClaim.status" : 1})"""
    Venue.where(_.legacyid eqs 1).select(_.claims.subselect(_.userid)).toString() must_== """db.venues.find({ "legid" : 1}, { "claims.uid" : 1})"""
Reply all
Reply to author
Forward
0 new messages