'How to insert documents into a Mongo Time Series collection using scala driver

I'm using the 4.5.1 version of the scala driver and trying to figure out if I can insert documents into a time series collection.

Im having a hard time finding good docs/examples on this, am not really happy with it, but i have come up with this so far. The problem I am seeing is

Write error: WriteError{code=2, message=''time' must be present and contain a valid BSON UTC datetime value', details={}}.

Any tips suggestions?

val doc: Document = Document(
      "_id"    -> 0,
      "time"   -> candle.date, //this is a long but not working
      "ticker" -> ticker,
      "open"   -> candle.open,
      "high"   -> candle.high,
      "low"    -> candle.low,
      "close"  -> candle.close
    )

    val f = collection
      .insertOne(doc)
      .toFuture()

f.onComplete {
      case Success(s)         => Console.println(s)
      case Failure(exception) => Console.println(exception)
    }




Solution 1:[1]

In case anyone else encounters this in the future, I was able to get it working with the BsonDateTime type:

val doc: Document = Document(
      "_id"    -> 0,
      "time"   -> BsonDateTime(candle.date * 1000), //date is in seconds, need millis
      "ticker" -> ticker,
      "open"   -> candle.open,
      "high"   -> candle.high,
      "low"    -> candle.low,
      "close"  -> candle.close
    )

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mattmar10