I am getting below exception : Exception in thread "main" org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class org.bson.BsonElement. at org.bson.codecs.configuration.CodecCache.getOrThrow(CodecCache.java:46) at org.bson.codecs.configuration.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:63) at org.bson.codecs.configuration.ChildCodecRegistry.get(ChildCodecRegistry.java:51) at org.mongodb.scala.bson.codecs.IterableCodec.org$mongodb$scala$bson$codecs$IterableCodec$$writeValue(IterableCodec.scala:71) at org.mongodb.scala.bson.codecs.IterableCodec$$anonfun$writeIterable$1.apply(IterableCodec.scala:87) at org.mongodb.scala.bson.codecs.IterableCodec$$anonfun$writeIterable$1.apply(IterableCodec.scala:87) at scala.collection.immutable.List.foreach(List.scala:381) at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35) at scala.collection.mutable.ListBuffer.foreach(ListBuffer.scala:45) at org.mongodb.scala.bson.codecs.IterableCodec.writeIterable(IterableCodec.scala:87) at org.mongodb.scala.bson.codecs.IterableCodec.org$mongodb$scala$bson$codecs$IterableCodec$$writeValue(IterableCodec.scala:69) at org.mongodb.scala.bson.codecs.IterableCodec.encode(IterableCodec.scala:58) at org.mongodb.scala.bson.codecs.IterableCodec.encode(IterableCodec.scala:51) at com.mongodb.client.model.BuildersHelper.encodeValue(BuildersHelper.java:37) at com.mongodb.client.model.Updates$SimpleUpdate.toBsonDocument(Updates.java:445) at com.mongodb.internal.operation.Operations.toBsonDocument(Operations.java:489) at com.mongodb.internal.operation.Operations.findOneAndUpdate(Operations.java:285) at com.mongodb.internal.operation.AsyncOperations.findOneAndUpdate(AsyncOperations.java:147)
Tried with applying different types of encoders but no success. Need how to use codec for BsonElement
Below is the code which gives above mentioned runtime Exception:
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)
Below code works :
val observableDoc = collection
.findOneAndUpdate(
equal("id", "1234"),
addToSet("ban_case_tkt", "test11"))
There is no Codec
for BsonElement
and that is why you are encountering an error. I would recommend just using a BsonDocument
otherwise you will have to create and register a custom Codec
.