0
我有一個用Scala編寫的應用程序來訪問Mongo數據庫,我可以在其中查詢和插入數據。Scala中的MongoDB編解碼器壞了?
然而,當我嘗試使用collection.distinct("user")
我得到的錯誤:
Unknown Error org.bson.codecs.configuration.CodecConfigurationException:
Can't find a codec for class scala.runtime.Nothing$.
做一些谷歌上搜索,我發現我需要指定的編解碼器後,所以我initalised的MongoClient
一個:
val clusterSettings = ClusterSettings
.builder()
.hosts(scala.collection.immutable.List(new com.mongodb.ServerAddress(addr)).asJava)
.build()
val settings = MongoClientSettings.builder()
.codecRegistry(MongoClient.DEFAULT_CODEC_REGISTRY)
.clusterSettings(clusterSettings)
.build()
val mongoClient: MongoClient = MongoClient(settings)
仍然沒有運氣,得到了同樣的錯誤。
我想我會需要做一個自定義的編解碼器as per this web page:
class NothingCodec extends Codec[Nothing] {
override def encode(writer:BsonWriter, value:Nothing, encoderContext:EncoderContext)={}
override def decode(reader: BsonReader, decoderContext: DecoderContext): Nothing = {
throw new Exception
}
override def getEncoderClass(): Class[Nothing] = {
classOf[Nothing]
}
}
但是,這並不工作,也沒有意義,沒有什麼是不是一個有效的返回類型; there exist no instances of this type。 因此,這obekouly不工作與Unknown Error java.lang.Exception
(顯然!)的新錯誤
我錯過了一些與Mongo Scala驅動程序?還是它剛剛壞了?
或者你可以看看http://reactivemongo.org/ – cchantep