Friday, November 9, 2018

CodecConfigurationException in mongo-java-driver when using $elemMatch in $all


https://stackoverflow.com/questions/53223909/codecconfigurationexception-in-mongo-java-driver-when-using-elemmatch-in-all

https://groups.google.com/forum/#!forum/mongodb-user


Who has the same error and how to fix it?
Java code:
// For simplify code I left one criteria elemMatch in all criteria.
Criteria expectedCriteria = Criteria.where("tags").all(new Criteria().elemMatch(Criteria.where("localization").is("EN")));
String json = expectedCriteria.getCriteriaObject().toJson();



Exception:
Exception in thread "main" org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class org.springframework.data.mongodb.core.query.Criteria.
    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.ProvidersCodecRegistry.get(ProvidersCodecRegistry.java:37)
    at org.bson.codecs.DocumentCodec.writeValue(DocumentCodec.java:184)
    at org.bson.codecs.DocumentCodec.writeIterable(DocumentCodec.java:207)
    at org.bson.codecs.DocumentCodec.writeValue(DocumentCodec.java:180)
    at org.bson.codecs.DocumentCodec.writeMap(DocumentCodec.java:199)
    at org.bson.codecs.DocumentCodec.writeValue(DocumentCodec.java:182)
    at org.bson.codecs.DocumentCodec.writeMap(DocumentCodec.java:199)
    at org.bson.codecs.DocumentCodec.encode(DocumentCodec.java:141)
    at org.bson.codecs.DocumentCodec.encode(DocumentCodec.java:45)
    at org.bson.Document.toJson(Document.java:313)
    at org.bson.Document.toJson(Document.java:286)
    at org.bson.Document.toJson(Document.java:273)


Version of mongo-java-driver in reproduce: (in all) - 3.4.3 - 3.5.0 - 3.6.4 - 3.7.1 - 3.8.2 - 3.9.0
Mongo DB script which works for code:
db.getCollection('devices').find({ 
    "tags": {
        "$all": [{
                "$elemMatch": {
                    "key": "localization",
                    "value": {
                        "$eq": "EN"
                    }
                }
            }
        ]
    }
})

No comments: