目次

Version 6, last updated by sorenbs at Nov 24 00:23 UTC

MongoDB Record Backend

Installation

SBT users

Add dependency to your project description:

val lift_mongo_record = "net.liftweb" % "lift-mongodb-record" % "2.0-XX"

Maven users

Add dependency to your pom:

   <dependency>
  <groupId>net.liftweb</groupId>
  <artifactId>lift-mongodb-record</artifactId>
  <version>2.0-XX</version>
</dependency>

Usage

See lift-mongodb for info on defining a MongoDB database and other low level operations.

See RecordExamples.scala for examples.

Define record classes:

class MainDoc extends MongoRecord[MainDoc] with MongoId[MainDoc] {
  def meta = MainDoc
  object name extends StringField(this, 12)
  object cnt extends IntField(this)
  object refdoc extends DBRefField[MainDoc, RefDoc](this, RefDoc)	
  object refdocId extends ObjectIdField(this) {
    def fetch = RefDoc.find(value)
  }
}
object MainDoc extends MainDoc with MongoMetaRecord[MainDoc] {
  def createRecord = new MainDoc
}
class RefDoc extends MongoRecord[RefDoc] with MongoId[RefDoc] {
  def meta = RefDoc
}
object RefDoc extends RefDoc with MongoMetaRecord[RefDoc] {
  def createRecord = new RefDoc
}

Create records and save:

val ref1 = RefDoc.createRecord
val ref2 = RefDoc.createRecord
ref1.save
ref2.save
val refString1 = RefStringDoc.createRecord
val refString2 = RefStringDoc.createRecord
refString1.save
refString2.save
val md1 = MainDoc.createRecord
val md2 = MainDoc.createRecord
val md3 = MainDoc.createRecord
val md4 = MainDoc.createRecord
md1.name.set("md1")
md2.name.set("md2")
md3.name.set("md3")
md4.name.set("md4")
md1.refdoc.set(ref1.getRef)
md2.refdoc.set(ref1.getRef)
md3.refdoc.set(ref2.getRef)
md4.refdoc.set(ref2.getRef)
md1.refdocId.set(ref1.id)
md2.refdocId.set(ref1.id)
md3.refdocId.set(ref2.id)
md4.refdocId.set(ref2.id)
md1.save must_== md1
md2.save must_== md2
md3.save must_== md3
md4.save must_== md4

Query:

val mdq1 = MainDoc.findAll(("name" -> "md1"))

Note that it is also possible to pass DBObject instances to find and findAll, for times where the standard query functionality or JSON DSL doesn’t support a specific use case.

toJs, toXhtml, and toForm have not been implemented.

Refences

mongodb-record tests

Mongo advanced queries