Does ReactiveMongo handle extended JSON to BSON conversion fully?

183 Views Asked by At

I have been trying to use reactivemongo to insert some documents into a mongodb collection with a few BSON types.

I am using the Play JSON library to parse and manipulate some documents in extended JSON, here is one example:

{
    "_id" : {"$oid": "5f3403dc7e562db8e0aced6b"},
    "some_datetime" : {
        "$date" : {"$date": 1597841586927}
    }
}

I'm using reactivemongo-play-json, and so I have to import the following so my JsObject is automatically cast to a reactivemongo BSONDocument when passing it to collection.insert.one

import reactivemongo.play.json.compat._
import json2bson._

Unfortunately, once I open my mongo shell and look at the document I just inserted, this is the result:

{
    "_id" : ObjectId("5f3403dc7e562db8e0aced6b"),
    "some_datetime" : {
        "$date" : NumberLong("1597244282116")
    },
}

Only the _id has been understood as a BSON type described using extended JSON, and I'd expect the some_datetime field to be something like a ISODate(), same as I'd expect to see UUID()-type values instead of their extended JSON description which looks like this:

{'$binary': 'oKQrIfWuTI6JpPbPlYGYEQ==', '$type': '04'}

How can I make sure this extended JSON is actually converted to proper BSON types?

1

There are 1 best solutions below

0
On BEST ANSWER

Turns out the problem is that what I thought to be extended JSON is actually not; my datetime should be formatted as:

{"$date": {"$numberLong": "1597841586927"}}

instead of

{"$date": 1597841586927}

The wrong format was introduced by my data source - a kafka connect mongo source connector not serializing documents to proper extended JSON by default (see this stackoverflow post).