Android Studio: BSON_CODEC_NOT_FOUND error when using "Updates" or "Filters"

Hello :slight_smile:

I am using mongo realm for my Android Studio application. When using .updateOne method to update a Document inside one of my “users” Collection, I use this code:

Bson filter = Filters.eq("_id", userid);
Bson update = Updates.combine(
      "numberToIncrement", 1),
                Updates.push("animalArray", "fish")
usersCollection.updateOne(filter , update).getAsync(task -> { ...

In Android Studio it shows me no errors, but when I try to run the code, the error for “Filters” comes:

BSON_CODEC_NOT_FOUND(realm::app::CustomError:1100): Could not resolve codec for SimpleEncodingFilter
org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for CodecCacheKey{clazz=class com.mongodb.client.model.Filters$SimpleEncodingFilter, types=null}.
    at org.bson.internal.ProvidersCodecRegistry.lambda$get$0$org-bson-internal-ProvidersCodecRegistry(

and a similar error emerges for the “Updates”-part when I replace “Bson filter = Filters.eq(”_id", userid);" with “Document filter = new Document(”_id", userid);":

BSON_CODEC_NOT_FOUND(realm::app::CustomError:1100): Could not resolve codec for CompositeUpdate
org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for CodecCacheKey{clazz=class com.mongodb.client.model.Updates$CompositeUpdate, types=null}.
    at org.bson.internal.ProvidersCodecRegistry.lambda$get$0$org-bson-internal-ProvidersCodecRegistry(

My specs:
Android Gradle Plugin Version 8.1.2 . Gradle Version 8.4 . Compile SDK Version 34. Java Version 17. For implementation I use in my build.gradle: implementation ‘org.mongodb:mongodb-driver-sync:4.10.2’

Additional information:
Before getting this error, I had to include in my build.gradle:

packagingOptions {
        exclude 'META-INF/native-image/org.mongodb/bson/'

because I got the error:

Execution failed for task ':app:mergeDebugJavaResource'.
> A failure occurred while executing
   > 2 files found with path 'META-INF/native-image/org.mongodb/bson/' from inputs:
      - E:\.gradle\caches\transforms-3\a6834347cc909f58f15e170b45aed9c5\transformed\jetified-mongodb-driver-core-4.11.0.jar
      - E:\.gradle\caches\transforms-3\7887ee97f50a4509db80ce209ca44106\transformed\jetified-bson-4.11.0.jar
     Adding a packagingOptions block may help, please refer to
     for more information

Can anyone help me in this ??? I am desperate and haven’t found helpful info about this affair yet.
Best regards and thanks in advance <3

17:37 CET just found the solution. The problem laid in the initialization of the usersCollection. Before, I just did:

usersCollection = mongoDatabase.getCollection("users");

Now, I changed the code to incorporate a CodecRegistry:

CodecRegistry defaultJavaCodecRegistry = MongoClientSettings.getDefaultCodecRegistry();
 usersCollection = mongoDatabase.getCollection("users").withCodecRegistry(defaultJavaCodecRegistry);

My god this solution might seem obvious to people, but I really have burned some hours to find that out. But there’s just no other people having that precitation online, so you gotta find out what is the cause yourself :wink:

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.