How do I get a Date to store as a Date in MongoDB instead of an Int64?

69 views
Skip to first unread message

Geoffrey Knauth

unread,
Dec 7, 2015, 4:42:09 PM12/7/15
to mongodb-user

I'm using the new MongoDB Scala Driver. When I store a java.util.Date, it is being stored in MongoDB as an Int64 instead of a MongoDB Date. I have some code that looks like this:


implicit val writer = new Writes[Forecast] {
  def writes(x: Forecast): JsValue = {
    Json.obj(
      // ...
      "issueDateTime" -> x.issueDateTime // which is a java.util.Date
      // ...
    )
  }
}

but what ends up in MongoDB is an Int64, not a Date. How do I get a Date into MongoDB?


(also posted to SO: http://stackoverflow.com/questions/34140698/how-do-i-get-a-date-to-store-as-a-date-in-mongodb-instead-of-an-int64 ]

Ross Lawley

unread,
Dec 8, 2015, 8:05:09 AM12/8/15
to mongodb-user
Hi Geoffrey,

I have a feeling its the Json / JsValue that is losing the type as Dates aren't a supported Json type.  How are you converting Json into Bson?

When constructing the native Bson Documents the Date will be preserved as expected eg:

val newdate = new Date()
val doc: Document = Document("test" -> newdate)

I hope that helps,

Ross

Geoffrey Knauth

unread,
Dec 11, 2015, 10:14:05 AM12/11/15
to mongodb-user
Thanks Ross.  I'm now building a Document (instead of Json/JsValue) using the BSON types and everything is working well.

Geoffrey
Reply all
Reply to author
Forward
0 new messages