Search code examples
mongodbscalareactivemongoplay-reactivemongo

Reactivemongo - BSONObjectID and BSONDateTime Insertion


I'm using play2.5 + play2-reactivemongo 0.12.3.

BSONObjectID is inserted as array and BSONDateTime is inserted as NumberLong on mongoDB with following codes.

> db.Example.find()
{ "_id" : { "array" : [ 89, 55, -10, 60, -40, 0, 0, -61, 0, -94, 126, 23 ] }, "created" : NumberLong("1496839744818") }

I want BSONObjectID to be inserted as ObjectID and DateTime to be inserted as BSONDateTime. Is it possible to do with type parameter?

TemporalModel.scala

trait TemporalModel {
  var _id: Option[BSONObjectID]
  var created: Option[DateTime]
}

Example.scala

import org.joda.time.DateTime
...

case class Example(
  var_id: Option[BSONObjectID],
  str: String,
  var created: Option[DateTime]
) extends TemporalModel

object Example {

  //implicit val objectIdRead: Reads[BSONObjectID] =
  val objectIdRead: Reads[BSONObjectID] =
    (__ \ "$oid").read[String].map { oid =>
      BSONObjectID(oid)
  }

  //implicit val objectIdWrite: Writes[BSONObjectID] = new Writes[BSONObjectID] {
  val objectIdWrite: Writes[BSONObjectID] = new Writes[BSONObjectID] {
    def writes(objectId: BSONObjectID): JsValue = Json.obj(
      "$oid" -> objectId.stringify
    )
  }

  //implicit val dateTimeRead: Reads[DateTime] =
  val dateTimeRead: Reads[DateTime] =
    (__ \ "$date").read[Long].map { dateTime =>
      new DateTime(dateTime)
  }

  //implicit val dateTimeWrite: Writes[DateTime] = new Writes[DateTime] {
  val dateTimeWrite: Writes[DateTime] = new Writes[DateTime] {
    def writes(dateTime: DateTime): JsValue = Json.obj(
      "$date" -> dateTime.getMillis
    )
  }

  implicit val objectIdFormats = Format(objectIdRead, objectIdWrite)
  implicit val dateTimeFormats = Format(dateTimeRead, dateTimeWrite)
  //implicit val bsonObjectIDJsonFormat = Json.format[BSONObjectID]
  implicit val exampleJsonFormat = Json.format[Example]

  def apply(str: String): Example = {
    new Example(null, str, null)
  }
}

BaseDAO.scala

class BaseDAO[T] {
  ...
  val collectionName: String
  lazy val collection: Future[JSONCollection] = reactiveMongoApi.database.map(_.collection(collectionName))

  def toJs[K : Writes](o: K) = Json.toJson(o).as[JsObject]

  def insert(document: T)(implicit writer: Writes[T]): Future[T] = {
    document._id = Some(BSONObjectID.generate)
    document.created = Some(DateTime.now)
    collection.flatMap(_.insert(toJs(document)))
  }
}

ExampleDAO.scala

trait ExampleDAO extends BaseDAO[Example]

class ExampleDAOImpl @Inject() (val reactiveMongoApi: ReactiveMongoApi) 
extends ExampleDAO {
  val collectionName = "example"
}

Solution

  • The problem is that you have too many implicits in-scope. However, since those are resolved in the macro, there seems to be no compilation error.

    What you should do:

    • remove all implicit modifiers in front of your Reads and Writes for BSONObjectID and DateTime
    • remove bsonObjectIDJsonFormat (which has same type as objectIdFormats, so this should not be allowed by the compiler)