[JAVA-2943] Unable to list all the documents in my collection. Created: 23/Aug/18  Updated: 11/Sep/19  Resolved: 12/Sep/18

Status: Closed
Project: Java Driver
Component/s: API
Affects Version/s: 3.8.1
Fix Version/s: None

Type: Task Priority: Critical - P2
Reporter: Durga Deep Assignee: Unassigned
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

mac



 Description   

 

I have a very simple POJO - where I don't have an _id field ( I let mongo generate the _id field for me ).

I am able to insert / update and delete successfully. But when i try to list out all the POJO instances in my collection 

package com.mongodb.reactivestreams.client;
 
/**
 * The MongoCollection interface.
 *
 * <p>Note: Additions to this interface will not be considered to break binary compatibility.</p>
 *
 * @param <TDocument> The type that this collection will encode documents from and decode documents to.
 * @since 1.0
 */
@ThreadSafe
public interface MongoCollection<TDocument> {
 
/**
 * Finds all documents in the collection.
 *
 * @return the fluent find interface
 * @mongodb.driver.manual tutorial/query-documents/ Find
 */
FindPublisher<TDocument> find();

I get the following error - 

We are using mongodb-driver-reactivestreams (1.9.0)

{{}}

 

org.bson.codecs.configuration.CodecConfigurationException: An exception occurred when decoding using the AutomaticPojoCodec.
Decoding into a 'FooBar' failed with the following exception:
Failed to decode 'FooBar'. Decoding '_id' errored with: readString can only be called when CurrentBSONType is STRING, not when CurrentBSONType is OBJECT_ID.
A custom Codec or PojoCodec may need to be explicitly configured and registered to handle this type.
 at org.bson.codecs.pojo.AutomaticPojoCodec.decode(AutomaticPojoCodec.java:40) ~[bson-3.7.1.jar:?]
 at com.mongodb.operation.CommandResultArrayCodec.decode(CommandResultArrayCodec.java:52) ~[mongodb-driver-core-3.8.0.jar:?]
 at com.mongodb.operation.CommandResultDocumentCodec.readValue(CommandResultDocumentCodec.java:60) ~[mongodb-driver-core-3.8.0.jar:?]
 at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:84) ~[bson-3.7.1.jar:?]
 at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:41) ~[bson-3.7.1.jar:?]
 at org.bson.codecs.configuration.LazyCodec.decode(LazyCodec.java:47) ~[bson-3.7.1.jar:?]
 at org.bson.codecs.BsonDocumentCodec.readValue(BsonDocumentCodec.java:101) ~[bson-3.7.1.jar:?]
 at com.mongodb.operation.CommandResultDocumentCodec.readValue(CommandResultDocumentCodec.java:63) ~[mongodb-driver-core-3.8.0.jar:?]
 at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:84) ~[bson-3.7.1.jar:?]
 at
Caused by: org.bson.codecs.configuration.CodecConfigurationException: Failed to decode 'FooBar'. Decoding '_id' errored with: readString can only be called when CurrentBSONType is STRING, not when CurrentBSONType is OBJECT_ID.
 at org.bson.codecs.pojo.PojoCodecImpl.decodePropertyModel(PojoCodecImpl.java:195) ~[bson-3.7.1.jar:?]
 at org.bson.codecs.pojo.PojoCodecImpl.decodeProperties(PojoCodecImpl.java:173) ~[bson-3.7.1.jar:?]
 at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:127) ~[bson-3.7.1.jar:?]
 at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:131) ~[bson-3.7.1.jar:?]
 at org.bson.codecs.pojo.AutomaticPojoCodec.decode(AutomaticPojoCodec.java:37) ~[bson-3.7.1.jar:?]
 ... 46 more
Caused by: org.bson.BsonInvalidOperationException: readString can only be called when CurrentBSONType is STRING, not when CurrentBSONType is OBJECT_ID.
 at org.bson.AbstractBsonReader.verifyBSONType(AbstractBsonReader.java:690) ~[bson-3.7.1.jar:?]
 at org.bson.AbstractBsonReader.checkPreconditions(AbstractBsonReader.java:722) ~[bson-3.7.1.jar:?]
 at org.bson.AbstractBsonReader.readString(AbstractBsonReader.java:457) ~[bson-3.7.1.jar:?]
 at org.bson.codecs.StringCodec.decode(StringCodec.java:39) ~[bson-3.7.1.jar:?]
 at org.bson.codecs.StringCodec.decode(StringCodec.java:28) ~[bson-3.7.1.jar:?]
 at org.bson.codecs.DecoderContext.decodeWithChildContext(DecoderContext.java:93) ~[bson-3.7.1.jar:?]
 at org.bson.codecs.pojo.PojoCodecImpl.decodePropertyModel(PojoCodecImpl.java:189) ~[bson-3.7.1.jar:?]
 at org.bson.codecs.pojo.PojoCodecImpl.decodeProperties(PojoCodecImpl.java:173) ~[bson-3.7.1.jar:?]
 at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:127) ~[bson-3.7.1.jar:?]
 at org.bson.codecs.pojo.PojoCodecImpl.decode(PojoCodecImpl.java:131) ~[bson-3.7.1.jar:?]
 at org.bson.codecs.pojo.AutomaticPojoCodec.decode(AutomaticPojoCodec.java:37) ~[bson-3.7.1.jar:?]
 ... 46 more

 



 Comments   
Comment by Ross Lawley [ 12/Sep/18 ]

Hi durgadeep,

I've closed this ticket as I think we identified the cause and workaround for your issue. Just to let you know for future reference this project is for Java driver bugs or feature requests. The best place for questions regarding MongoDB usage or the Java driver specifics is the mongodb-user mailinglist or stackoverflow as you will reach a boarder audience there. If your business requires an answer from MongoDB within a time frame then we do offer production support.

If you do follow up via one of the options above please post a link and I will follow the conversation there.

All the best,

Ross

Comment by Ross Lawley [ 03/Sep/18 ]

Hi durgadeep,

This should automatically be handled by the CLASS_AND_PROPERTY_CONVENTION. If you are using the default conventions then it is likely that the document was added to the collection without a set id property and an ObjectId was created. Could this be the case?

As mentioned previously, you may need a custom codec to handle the converstion of ObjectId's to Strings - or just use ObjectIds if you don't require a String primary key.

Kind Regards,

Ross

Comment by Durga Deep [ 30/Aug/18 ]

You're spot on - here is my POJO

 

class FooBar {
    private String id;
    private String title;
}

 

When I persist - I am setting the id - but this is not the _id that is auto generated.

How do I solve this ?.

  • One way is to override the _id with the id that is being uniquely generated in our application ( but how can I do that ).

Thanks Much

_Durga Deep

Comment by Ross Lawley [ 29/Aug/18 ]

Hi durgadeep,

Can you please supply the FooBar POJO? From the error it looks like you have an id property that is a String but when decoding the PojoCodec found a ObjectID value and has errored.

If you may need a custom codec to handle the converstion of ObjectId's to Strings - or just use ObjectIds if you don't require a String primary key.

Ross

Generated at Thu Feb 08 08:58:25 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.