Mongo-Scala-Driver:CodecConfigurationException:找不到类immutable.Document的编解码器

时间:2017-03-27 18:43:14

标签: mongodb scala mongo-scala-driver

错误讯息:

`Int' is applied to too many type arguments
In the type signature for `_sum_divide':
  _sum_divide :: Num a => [a] -> Int b => b -> b -> b

代码:

org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class org.mongodb.scala.bson.collection.immutable.Document

跟:

def queueWrite(collection: String, filter: Map[String, () => String], data: Map[String, () => String]) {
    val col = collections.get(collection).get

    val filterBson = Document()
    filter.foreach(f => { filterBson.append(f._1, f._2.apply) })

    val dataBson = Document()
    data.foreach(f => { dataBson.append(f._1, f._2.apply) })

    val options = new FindOneAndUpdateOptions
    options.returnDocument(ReturnDocument.AFTER)
    options.upsert(true)

    val observer = new Observer[Document] {
      override def onNext(doc: Document) = println(doc.toJson)
      override def onError(e: Throwable) = e.printStackTrace
      override def onComplete = println("onComplete")
    }

    val observable: Observable[Document] = col.findOneAndUpdate(filterBson, dataBson, options)
    observable.subscribe(observer)

  }

我尝试过使用可变文档但后来意识到findoneandupdate返回一个不可变的文档。我也尝试使用BsonDocument过滤器,但是ofc没有效果。我不确定从哪里开始,任何帮助将不胜感激:)

1 个答案:

答案 0 :(得分:2)

private val settings = MongoClientSettings.builder
    .clusterSettings(clusterSettings)
    .build

我的MongoClientSettings之前看起来像这样,我需要将其更改为:

private val settings = MongoClientSettings.builder
    .clusterSettings(clusterSettings)
    .codecRegistry(MongoClient.DEFAULT_CODEC_REGISTRY)
    .build

似乎mongo没有假设默认编解码器注册表

感谢@Ross的帮助!