Advertisement
Guest User

Oh yis

a guest
Jul 24th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 0.82 KB | None | 0 0
  1.   case class SimpleAlbum(
  2.     title: String,
  3.     releaseYear: Int)
  4.  
  5.   var test = new SimpleAlbum("Mago de oz", 1900)
  6.  
  7.   implicit object SimpleAlbumWriter extends BSONDocumentWriter[SimpleAlbum] {
  8.     def write(album: SimpleAlbum): BSONDocument = BSONDocument(
  9.       "title" -> album.title,
  10.       "releaseYear" -> album.releaseYear)
  11.   }
  12.  
  13.   implicit object SimpleAlbumReader extends BSONDocumentReader[SimpleAlbum] {
  14.     def read(doc: BSONDocument): SimpleAlbum = {
  15.       SimpleAlbum(
  16.         doc.getAs[String]("title").get,
  17.         doc.getAs[Int]("releaseYear").get)
  18.     }
  19.   }
  20.  
  21.   val future = collection1.insert(SimpleAlbumWriter.write(test))
  22.  
  23.   future.onComplete {
  24.     case Failure(e) => throw e
  25.     case Success(lastError) => {
  26.       println("successfully inserted document with lastError = " + lastError)
  27.     }
  28.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement