Uploaded image for project: 'Java Driver'
  1. Java Driver
  2. JAVA-2386

Exception when a null value is included in the results of the distinct command

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 3.4.0
    • Affects Version/s: 3.3.0
    • Component/s: Query Operations
    • None
    • Environment:
      MongoDB server 3.2.8

      Windows 7 x64
      java version "1.8.0_45"
      Java(TM) SE Runtime Environment (build 1.8.0_45-b15)
      Java HotSpot(TM) 64-Bit Server VM (build 25.45-b02, mixed mode)

      Using the async Java driver and running a `distinct` query over a string-typed field that may contain null values results in `org.bson.BsonInvalidOperationException` being thrown. Example code using the `distinct` query operation, sanitized and simplified:

      mongoCollection
        .distinct("stringFieldName", String.class)
        .into(new ArrayList<String>(), asyncCallback)
      

      A workaround is available to filter out null values:

      mongoCollection
        .distinct("stringFieldName", Filters.ne("stringFieldName", new BsonNull()), String.class)
        .into(new ArrayList<String>(), asyncCallback)
      

      Exception:

      org.bson.BsonInvalidOperationException: readString can only be called when CurrentBSONType is STRING, not when CurrentBSONType is NULL.
      	at org.bson.AbstractBsonReader.verifyBSONType(AbstractBsonReader.java:655)
      	at org.bson.AbstractBsonReader.checkPreconditions(AbstractBsonReader.java:687)
      	at org.bson.AbstractBsonReader.readString(AbstractBsonReader.java:428)
      	at org.bson.codecs.StringCodec.decode(StringCodec.java:39)
      	at org.bson.codecs.StringCodec.decode(StringCodec.java:28)
      	at com.mongodb.operation.CommandResultArrayCodec.decode(CommandResultArrayCodec.java:48)
      	at com.mongodb.operation.CommandResultDocumentCodec.readValue(CommandResultDocumentCodec.java:53)
      	at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:84)
      	at org.bson.codecs.BsonDocumentCodec.decode(BsonDocumentCodec.java:41)
      	at com.mongodb.connection.ReplyMessage.<init>(ReplyMessage.java:57)
      	at com.mongodb.connection.CommandProtocol.getResponseDocument(CommandProtocol.java:139)
      

            Assignee:
            ross@mongodb.com Ross Lawley
            Reporter:
            jim.dunne Jim Dunne
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: