Question about BSONWriter in example app.

116 views
Skip to first unread message

Andrew James Ramirez

unread,
Apr 7, 2014, 2:17:30 AM4/7/14
to reacti...@googlegroups.com
  implicit object ArticleBSONReader extends BSONDocumentReader[Article] {
    def read(doc: BSONDocument): Article =
      Article(
        doc.getAs[BSONObjectID]("_id"),
        doc.getAs[String]("title").get,
        doc.getAs[String]("content").get,
        doc.getAs[String]("publisher").get,
        doc.getAs[BSONDateTime]("creationDate").map(dt => new DateTime(dt.value)),
        doc.getAs[BSONDateTime]("updateDate").map(dt => new DateTime(dt.value)))
  }
  implicit object ArticleBSONWriter extends BSONDocumentWriter[Article] {
    def write(article: Article): BSONDocument =
      BSONDocument(
        "_id" -> article.id.getOrElse(BSONObjectID.generate),
        "title" -> article.title,
        "content" -> article.content,
        "publisher" -> article.publisher,
        "creationDate" -> article.creationDate.map(date => BSONDateTime(date.getMillis)),
        "updateDate" -> article.updateDate.map(date => BSONDateTime(date.getMillis)))
  }

 I got a little confused on why do you need to have 

 "_id" -> article.id.getOrElse(BSONObjectID.generate)

if your just using the Article object as a cursor for querying. 

val found = collection.find(query).cursor[Article]  


All of the code are base on the example project provided in the site

Message has been deleted

Andrew James Ramirez

unread,
Apr 7, 2014, 3:22:53 AM4/7/14
to reacti...@googlegroups.com
And in the example he did a custom Apply and Unapply method where he generate a objectId 

new BSONObjectID(_)

is this best practice? To make sure that objectId will never be null? So he generate a id in formbinding + in BSONWriter he also tried to generate a id if nothing found. But isn't it a bit redundant?

val form = Form(
    mapping(
      "id" -> optional(of[String] verifying pattern(
        """[a-fA-F0-9]{24}""".r,
        "constraint.objectId",
        "error.objectId")),
      "title" -> nonEmptyText,
      "content" -> text,
      "publisher" -> nonEmptyText,
      "creationDate" -> optional(of[Long]),
      "updateDate" -> optional(of[Long])) { (id, title, content, publisher, creationDate, updateDate) =>
        Article(
          id.map(new BSONObjectID(_)),
          title,
          content,
          publisher,
          creationDate.map(new DateTime(_)),
          updateDate.map(new DateTime(_)))
      } { article =>
        Some(
          (article.id.map(_.stringify),
            article.title,
            article.content,
            article.publisher,
            article.creationDate.map(_.getMillis),
            article.updateDate.map(_.getMillis)))
      })

Stephane Godbillon

unread,
Apr 16, 2014, 3:58:52 AM4/16/14
to reacti...@googlegroups.com
Hi,

Readers and Writers are used mainly to serialize and deserialize classes, not to produce the selector document.

The form id is mapped this way to handle the case where the id is missing in the request body (which often happens when you want to create a document, since you don't have an id yet.) So if there is an id, a Some[BSONObjectID] is produced, else None is returned. I don't generate any id here, only transform an id (of type string) into a BSONObjectID instance when it exists.

Cheers,
--
You received this message because you are subscribed to the Google Groups "ReactiveMongo - http://reactivemongo.org" group.
To unsubscribe from this group and stop receiving emails from it, send an email to reactivemong...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages