When working with document-based databases such as MongoDB, instead of using a one-to-many relationship to link to classes that represent two tables in the database—such as contacts
and phones
—we can map both objects Contact
and Phone
. Then, instead of storing them in two different collections, we embed the phone
object into the contact
object. In the following recipe, we will learn how to embed one object into another.
Carry out the following steps:
Create a new file named
Phone.scala
insrc/main/scala/code/model
with the following code:package code.model import net.liftweb.mongodb.record._ import net.liftweb.record.field.StringField class Phone extends BsonRecord[Phone] { def meta = Phone val number = new StringField(this, 15) } object Phone extends Phone with BsonMetaRecord[Phone]
Change the
Contact
class by adding thenumbers
object as follows:class Contact extends MongoRecord...