In the previous recipe, we learned how to connect a Lift application to a MongoDB database. Now, we will learn how to map a collection from MongoDB to a Scala class so we can use it to store and retrieve data into and from the MongoDB collection.
Let's modify the project from the previous recipe because it already has the connection configured.
Carry out the following steps:
Create a file named
Contact.scala
in themodel
package using the following code:package code.model import net.liftweb.mongodb.record._ import net.liftweb.mongodb.record.field._ import net.liftweb.record.field.StringField class Contact extends MongoRecord[Contact] with ObjectIdPk[Contact] { def meta = Contact object name extends StringField(this, 150) } object Contact extends Contact with MongoMetaRecord[Contact]
Create a file named
Contacts.scala
in thesnippet
package with the following code:package code.snippet import code.model._ import...