错误信息:
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 作为过滤器,但那个 ofc 没有效果。我不太确定从这里去哪里,任何帮助将不胜感激:)