I'm trying to store java.time.Instant
in mongo's native Date
format.
Official ReactiveMongo documentation states that date time should be serialized as
JsObject with a $date JsNumber field with the timestamp (milliseconds) as value
(http://reactivemongo.org/releases/1.0/documentation/json/overview.html)
I follow this rule, but the value is not stored as mongo's Date
, instead, it is stored as a regular JSON (BSON) document:
{
"_id" : ObjectId("6057b962af0000af00e81ec7"),
"username" : "john",
"createdAt" : {
"$date" : NumberLong(1616361826198)
}
}
Scala source code that stores the document:
import play.api.libs.json.Json
import reactivemongo.api.DB
import reactivemongo.api.bson.BSONObjectID
import reactivemongo.api.bson.collection.BSONCollection
import java.time.Instant
import scala.concurrent.{ ExecutionContext, Future }
import reactivemongo.play.json.compat._
import json2bson._
import reactivemongo.api.commands.WriteResult
class Repo(database: DB)(implicit ec: ExecutionContext) {
def collection: BSONCollection =
database.collection("users")
def insertDocument(): Future[WriteResult] = {
val doc = Json.obj(
"_id" -> BSONObjectID.generate(),
"username" -> "john",
"createdAt" -> Json.obj("$date" -> Instant.now().toEpochMilli)
)
collection.insert.one(doc)
}
}
What's wrong here?
P.S.:
If I change extended BSON syntax
Json.obj("$date" -> Instant.now().toEpochMilli)
to BSONDateTime
:
...
"createdAt" -> BSONDateTime(Instant.now().toEpochMilli)
...
it works.
But still, why doesn't it work with play JSON + extended syntax?