expecting to save Double in the db but mongo decide to save it as Int64 (big number)

148 Views Asked by At

im using unit test to check what type will be inserted to the db, my code is simple:

"insert big double to mongo" in {
  val doc: MyDocument = generateMyDocument()
  val newdoc = doc.copy(myDoubleField = 2394735300.0)
  dao.insertOne(newdoc, action).futureValue
}

this will insert int64 for the field myDoubleField which supposed to be Double..

if I change the number to be 24,735,300 it get saved as int32..

if I change the number to 2,735,300 it get saved as Double!

this is very weird to me I don't understand.

my insertOne method looks like:

  def insertOne(doc: Doc, action: EventAction): Future[Doc] = {
    doc.createdAt = Some(InstantNow)
    doc.updatedAt = doc.createdAt
    (for {
      insertedDoc <- collection.insert.one(doc)
        .map({
        case DefaultWriteResult(true, 1, _, _, _, _) => doc
        case dwr => throw new RuntimeException(s"$LogName was not inserted, something went wrong: $dwr")
      })
      _ <- eventsCollection.insert.one(EventDocument(action, DataOperation.Create, dataType, None, doc._id, doc.metadata, None, None, None))
    } yield insertedDoc)
      .recover({
        case WriteResult.Code(DuplicateCode) => throw DuplicateKeyException(doc.uniqueIndexToString)
      })
  }

please, how do I ensure I save it as Double, thanks!

0

There are 0 best solutions below