通过实现编解码器解决的问题scala.math.BigDecimal
:
class BigDecimalScalaCodec extends Codec[scala.math.BigDecimal] {
override def encode(writer: BsonWriter, value: scala.math.BigDecimal, encoderContext: EncoderContext): Unit = {
writer.writeDecimal128(new Decimal128(value.bigDecimal))
}
override def getEncoderClass: Class[scala.math.BigDecimal] = classOf[scala.math.BigDecimal]
override def decode(reader: BsonReader, decoderContext: DecoderContext): scala.math.BigDecimal = {
reader.readDecimal128().bigDecimalValue()
}
}
在 mongo 中注册:
val codecRegistry = fromRegistries(fromCodecs(new BigDecimalScalaCodec()), DEFAULT_CODEC_REGISTRY)
val database: MongoDatabase = mongoClient.getDatabase("dbName")
.withCodecRegistry(codecRegistry)
现在我们可以使用scala.math.BigDecimal
:
collection.findOneAndUpdate(filters,Updates.inc("someOtherKey", BigDecimal(99))