简体   繁体   中英

MongoDB exception using Java driver

Hey so I am trying to write the following mongoDB using the Java driver:

db.testDB.find( {$and : [ {EntryFee : {"$lte" : 15} }, {EntryFee : {"$gte" : 10} } ] } ) 

The java code I have looks like this, where minEntryFee and maxEntryFee are both Integers.

if(minEntryFee != null && maxEntryFee != null ){
        BasicDBList list = new BasicDBList();
        list.add(BasicDBObjectBuilder.start().push("EntryFee").add("$gte", minEntryFee));
        list.add(BasicDBObjectBuilder.start().push("EntryFee").add("$lte", maxEntryFee));
        builder.add("$and", list);
        return builder.get();
    }

But when I run this code, I get the following error:

Can't find a codec for class com.mongodb.BasicDBObjectBuilder.] with root cause org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class com.mongodb.BasicDBObjectBuilder.

Any ideas? Thanks.

When you send a request via Java Driver it tries to serialize request into BSON format and in case if it encounters object with unknown codec it throws the exception you mentioned above.

In order to fix your error you can try to change:

BasicDBObjectBuilder.start().push("EntryFee").add("$gte", minEntryFee)

to

BasicDBObjectBuilder.start().push("EntryFee").add("$gte", minEntryFee).get()

As far as first line returns not DBObject but BasicDBObjectBuilder which don't have any related codecs in the driver.

Thanks, Yevhenii

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM