Updating an embedded document (MongoJsonObjectField)

56 views
Skip to first unread message

Maarten Koopmans

unread,
Oct 15, 2012, 5:45:59 AM10/15/12
to Lif...@googlegroups.com
Hi,

Subjects says it hopefully all: what is the most elegant way to update
(i.e. add/remove items to/from) a MongoJsonObjectField?

I only found solutions that force you to build new lists on the client
side - which I hoped to avoid. Because that would be quite dangerous
wrt locking etc (concurrent updates).

Any feedback appreciated - and I'll add it to the Wiki as soon as I
figure it out.

Thanks!

--Maarten

Tim Nelson

unread,
Oct 15, 2012, 9:06:55 AM10/15/12
to lif...@googlegroups.com, Lif...@googlegroups.com
Hi Maarten,

If all you're updating is the one column, I would say use an update statement.

Tim

Maarten Koopmans

unread,
Oct 15, 2012, 10:34:30 AM10/15/12
to lif...@googlegroups.com
Hi Tim,

I have trouble seeing how that works (nor could I find an example)
from Record. Or should I code that query directly as a string (in
which case I have trouble seeing how that translates/serializes to
MongoJsonObjectField[MyClass])?

Thanks,

Maarten
> --
> --
> Lift, the simply functional web framework: http://liftweb.net
> Code: http://github.com/lift
> Discussion: http://groups.google.com/group/liftweb
> Stuck? Help us help you:
> https://www.assembla.com/wiki/show/liftweb/Posting_example_code
>
>
>

Tim Nelson

unread,
Oct 15, 2012, 3:47:29 PM10/15/12
to lif...@googlegroups.com
Hi Maarten,

If you have the following:

case class TypeTestJsonObject(
  intField: Int,
  stringField: String
) extends JsonObject[TypeTestJsonObject]
{
  def meta = TypeTestJsonObject
}
object TypeTestJsonObject extends JsonObjectMeta[TypeTestJsonObject]

class ListTestRecord private () extends MongoRecord[ListTestRecord] with UUIDPk[ListTestRecord] {
  def meta = ListTestRecord

  object mandatoryMongoJsonObjectListField extends MongoJsonObjectListField(this, TypeTestJsonObject)

}
object ListTestRecord extends ListTestRecord with MongoMetaRecord[ListTestRecord]

To add an item to the field:

import net.liftweb.json._
import net.liftweb.json.JsonDSL._

val rec = ListTestRecord.find(...)
val newObj = TypeTestJsonObject(1, "a")

val qry: JValue = ("_id" -> ....)
val upd: JValue = ("$push" -> ("mandatoryMongoJsonObjectListField" -> newObj.asJObject))

rec.update(qry, upd)

To remove an item:

val upd: JValue = ("$pull" -> ("mandatoryMongoJsonObjectListField" -> newObj.asJObject))

rec.update(qry, upd)

I've been thinking of adding these functions to the ListFields but haven't gotten around to it.

Rogue has a much better way of doing this, btw. If you haven't looked into that, I highly recommend it. Not sure if MongoJsonObjectListField is supported, though.

Tim

Maarten Koopmans

unread,
Oct 16, 2012, 1:32:46 AM10/16/12
to lif...@googlegroups.com
Hi Tim,

Awesome, thanks! Precisely what I needed. I'll try and merge it in the wiki.

Rogue looks very nice, but between Lift 2.5M1 and Rogue 2.0 I'm a bit wary. If someone has instructions I'll try and put it in the wiki as well.

Thanks again!

Maarten

Maarten Koopmans

unread,
Oct 17, 2012, 5:19:14 AM10/17/12
to lif...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages