当前位置: 首页 > 知识库问答 >
问题:

mongo-scala-driver:CodecConfigurationException:找不到类immutable.document的编解码器

季嘉良
2023-03-14
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)

  }
val filter = Map[String, () => String]("uuid", p.getUniqueId.toString)

var dataMap = Map[String, () => String]()
dataMap = dataMap.+("uuid" -> p.getUniqueId.toString)
dataMap = dataMap.+("nickname" -> p.getDisplayName)

queueWrite("players", filter, dataMap)

共有1个答案

徐丰茂
2023-03-14
private val settings = MongoClientSettings.builder
    .clusterSettings(clusterSettings)
    .build

我的MongoClientSettings以前是这样的,我需要将它更改为:

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

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

感谢@Ross的帮助!

 类似资料: