我是Scala的mongodb新手。我正在尝试获取记录中的所有distinc id,并且应该获取[id1,id2,id3,...,idn]
我的代码
val mongoClient: MongoClient = MongoClient()
val db: MongoDatabase = mongoClient.getDatabase("myDB")
val collection = db.getCollection("Datalake")
val response = Await.result(collection.distinct[Double]("idWell").toFuture(), Duration(5, TimeUnit.SECONDS))
if (response != null && response.nonEmpty) {
response.foreach(println(_))
}
我在线程“主”中得到异常org.bson.codecs.configuration.CodecConfigurationException:找不到用于double的编解码器。我是否需要在数据库连接器中配置任何内容?那个代码是正确的还是我应该用来理解未来?无论哪种方式,我都会得到相同的错误
输入数据应类似于以下内容(数组只是一个示例,记录是MongoDB中的文档):
[
{
"_id": "[field id]",
"oilFieldId": "[the id I'm using which represents an oil field id]",
"tef":"[a number]",
"agg":"[the field I'd like to update (currently = '')]",
"date": "[year-month-day]"
},
{
"_id": "[another field id]",
"oilFieldId": "[the id I'm using which represents an oil field id]",
"tef":"[a number]",
"agg":"[the field I'd like to update (currently = '')]",
"date": "[year-month-day]"
},
{
"_id": "[another field id]",
"oilFieldId": "[the id I'm using which represents an oil field id]",
"tef":"[a number]",
"agg":"[the field I'd like to update (currently = '')]",
"date": "[year-month-day]"
},
...
]
输出应为“ oilFieldId”的数组
[[1232345,135245,123155,24524,...]
发现错误。问题是返回值的编解码器,它实际上不是Double而是BsonInt32.type。这就是为什么在org.bson.codecs中找不到编解码器的原因
我已经完成了对理解的理解,并从Future.onComplete获得了价值。这是代码
val d = for(
a <- collection.distinct[BsonInt32]("oilFieldId").toFuture()
)yield a
d.onComplete{
case Success(element) => element.foreach{
System.out.println(_)
}
case Failure(t) => System.out.println(t.getMessage)
}