Slide 82
Slide 82 text
MongoDB from Scala
(casbah)
1 import com.mongodb.casbah.Imports._
2
3 case class Book(id: ObjectId, author: Seq[Author], isbn: String,
4 price: Price, publicationYear: Int, tags: Seq[String],
5 title: String, publisher: String, edition: Option[String]) {
6
7 def toDBObject = MongoDBObject(
8 "author" -> author.map { a => a.name },
9 "_id" -> id,
10 "isbn" -> isbn,
11 "price" -> price.toDBObject,
12 "publicationYear" -> publicationYear,
13 "tags" -> tags,
14 "title" -> title,
15 "publisher" -> publisher,
16 "edition" -> edition
17 )
18 }
19
20 case class Author(name: String)
21
22 case class Price(currency: String, discount: Double, msrp: Double) {
23 def toDBObject = MongoDBObject(
24 "currency" -> currency,
25 "discount" -> discount,
26 "msrp" -> msrp
27 )
28 }
29
Tuesday, November 15, 11