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

标签 mongodb scala mongo-scala-driver

错误信息:

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)


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

最佳答案

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

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

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

似乎 mongo 没有采用默认编解码器注册表

感谢@Ross 的帮助!

关于mongodb - Mongo-Scala-Driver:CodecConfigurationException:找不到类 immutable.Document 的编解码器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43054125/

相关文章:

java - 立即将 Spark 中的 Json 字符串索引到 Elasticsearch

javascript - Remove() 在 mongodb 中不起作用?

Scala 在运行时从字符串创建方法并调用它

javascript - mongo查询来自环回问题

scala - 如何使用 Scala 获取文件的创建日期

mongodb - 不坚持 Scala None's 而不是坚持为空值

Mongodb scala 驱动程序不插入一个文档

mongodb - 如何在 java/scala 中使用 inc 运算符创建 Decimal128 字段

php - MongoDB 选择排序问题

Java&Mongo : get object where field exist