线程“主”中的异常org.bson.codecs.configuration.CodecConfigurationException:找不到类org.bson.BsonElement的编解码器

时间:2019-03-27 15:20:34

标签: mongodb scala apache-spark mongo-scala-driver

我得到以下例外: 线程“主”中的异常org.bson.codecs.configuration.CodecConfigurationException:找不到类org.bson.BsonElement的编解码器。     在org.bson.codecs.configuration.CodecCache.getOrThrow(CodecCache.java:46)     在org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:63)     在org.bson.codecs.configuration.ChildCodecRegistry.get(ChildCodecRegistry.java:51)     位于org.mongodb.scala.bson.codecs.IterableCodec.org $ mongodb $ scala $ bson $ codecs $ IterableCodec $$ writeValue(IterableCodec.scala:71)     在org.mongodb.scala.bson.codecs.IterableCodec $$ anonfun $ writeIterable $ 1.apply(IterableCodec.scala:87)     在org.mongodb.scala.bson.codecs.IterableCodec $$ anonfun $ writeIterable $ 1.apply(IterableCodec.scala:87)     在scala.collection.immutable.List.foreach(List.scala:381)     在scala.collection.generic.TraversableForwarder $ class.foreach(TraversableForwarder.scala:35)     在scala.collection.mutable.ListBuffer.foreach(ListBuffer.scala:45)     在org.mongodb.scala.bson.codecs.IterableCodec.writeIterable(IterableCodec.scala:87)     位于org.mongodb.scala.bson.codecs.IterableCodec.org $ mongodb $ scala $ bson $ codecs $ IterableCodec $$ writeValue(IterableCodec.scala:69)     在org.mongodb.scala.bson.codecs.IterableCodec.encode(IterableCodec.scala:58)     在org.mongodb.scala.bson.codecs.IterableCodec.encode(IterableCodec.scala:51)     在com.mongodb.client.model.BuildersHelper.encodeValue(BuildersHelper.java:37)     在com.mongodb.client.model.Updates $ SimpleUpdate.toBsonDocument(Updates.java:445)     在com.mongodb.internal.operation.Operations.toBsonDocument(Operations.java:489)     在com.mongodb.internal.operation.Operations.findOneAndUpdate(Operations.java:285)     在com.mongodb.internal.operation.AsyncOperations.findOneAndUpdate(AsyncOperations.java:147)

尝试使用不同类型的编码器,但没有成功。需要如何为BsonElement使用编解码器

下面是给出上述运行时异常的代码:

        val mongoClient= MongoClient(uriString)
        val db = mongoClient.getDatabase(databaseName)
        val collection = db.getCollection(collectionName)
        var caseDBObj = new ListBuffer[BsonElement]()
        caseDBObj += new BsonElement("Key1", new BsonString("Value1"))
        caseDBObj += new BsonElement("Key2", new BsonString("Value2"))
        caseDBObj += new BsonElement("Key3", new BsonString("Value3"))
        val observableDoc = collection
        .findOneAndUpdate(
            equal("id", "1234"), 
            addToSet("ban_case_tkt", caseDBObj)
         )
         observableStatus(observableDoc)
         val awaitedR = Await.result(observableDoc.toFuture, Duration.Inf) 

以下代码有效:

            val observableDoc = collection
            .findOneAndUpdate(
            equal("id", "1234"), 
            addToSet("ban_case_tkt", "test11"))

1 个答案:

答案 0 :(得分:0)

Codec中没有BsonElement,这就是您遇到错误的原因。我建议仅使用BsonDocument,否则您将不得不创建并注册自定义Codec