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

Bson Filter behavior differs from BasicDBObject Filter

    • Type: Icon: Bug Bug
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 3.12.5
    • Component/s: Codecs, Query Operations
    • None
    • Environment:
      MacOS Mojave 10.14.6, Java 1.8, Driver 3.12.5, Mongo 3.6.17-ent single node replicaset

      Working with customer to upgrade a data access layer Java class.  Trying to implement consistency across the board.  I have elected to use the Bson query helpers defined in "org.bson.conversions.Bson.Filters".  I am converting use of BasicDBObject and encountered a difference.  For this reason I am creating this ticket.  This might be a known behavior or might be a discovered bug - I will let you decide!  Here are the details...

       

      Consider the following unit test...

       

      @Test
      public void mytest()
      {
       com.mongodb.MongoClientSettings mongoClientSettings = com.mongodb.MongoClientSettings.builder()
       .applyConnectionString(new com.mongodb.ConnectionString("mongodb://testuser:mysecret@localhost:27017/?replicaSet=replSet&w=majority&readConcernLevel=majority&readPreference=primary&authSource=admin&retryWrites=true&maxPoolSize=10&waitQueueTimeoutMS=1000"))
       .build();
      
       com.mongodb.client.MongoClient client = com.mongodb.client.MongoClients.create(mongoClientSettings);
       com.mongodb.client.MongoDatabase db = client.getDatabase("javatest");
       com.mongodb.client.MongoCollection<org.bson.Document> collection = db.getCollection("troubleshooting");
       collection.deleteMany(new org.bson.BsonDocument());
       org.bson.Document insertedDocument = new org.bson.Document();
       insertedDocument.append("field1", 1);
       collection.insertOne(insertedDocument);
       Object value = new Object();
       value = new Integer[] {4, 5, 6};
      
       BasicDBObject filter = new BasicDBObject("field1", new BasicDBObject("$nin", value));
      
       com.mongodb.client.FindIterable<org.bson.Document> iterable1 = collection.find(filter);
       try(com.mongodb.client.MongoCursor<org.bson.Document> cursor1 = iterable1.iterator())
       {
       while (cursor1.hasNext())
       {
       org.bson.Document queriedDocument1 = cursor1.next();
       System.out.println(String.format("queriedDocument1: %s", queriedDocument1));
       }
       }
      }

       

      If I change the filter predicate to use the Filters helper Bson object..

       

      @Test
      public void mytest()
      {
       com.mongodb.MongoClientSettings mongoClientSettings = com.mongodb.MongoClientSettings.builder()
       .applyConnectionString(new com.mongodb.ConnectionString("mongodb://testuser:mysecret@localhost:27017/?replicaSet=replSet&w=majority&readConcernLevel=majority&readPreference=primary&authSource=admin&retryWrites=true&maxPoolSize=10&waitQueueTimeoutMS=1000"))
       .build();
      
       com.mongodb.client.MongoClient client = com.mongodb.client.MongoClients.create(mongoClientSettings);
       com.mongodb.client.MongoDatabase db = client.getDatabase("javatest");
       com.mongodb.client.MongoCollection<org.bson.Document> collection = db.getCollection("troubleshooting");
       collection.deleteMany(new org.bson.BsonDocument());
       org.bson.Document insertedDocument = new org.bson.Document();
       insertedDocument.append("field1", 1);
       collection.insertOne(insertedDocument);
       Object value = new Object();
       value = new Integer[] {4, 5, 6};
      
       org.bson.conversions.Bson filter = com.mongodb.client.model.Filters.nin("field1", value);
      
       com.mongodb.client.FindIterable<org.bson.Document> iterable1 = collection.find(filter);
       try(com.mongodb.client.MongoCursor<org.bson.Document> cursor1 = iterable1.iterator())
       {
       while (cursor1.hasNext())
       {
       org.bson.Document queriedDocument1 = cursor1.next();
       System.out.println(String.format("queriedDocument1: %s", queriedDocument1));
       }
       }
      }

      {{ I get a codec error org.bson.codecs.configuration.CodecConfigurationException: Can't find a codec for class [Ljava.lang.Integer;.}}

       

      Please advise...

            Assignee:
            brian.deleonardis@mongodb.com Brian DeLeonardis (Inactive)
            Reporter:
            barron.anderson@mongodb.com Barron Anderson
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

              Created:
              Updated:
              Resolved: