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

Filters can produce invalid query shapes in Java 4.0+

    XMLWordPrintable

Details

    • Bug
    • Status: Closed
    • Major - P3
    • Resolution: Won't Fix
    • None
    • None
    • Builders
    • None

    Description

      The Filters produce different query shapes between versions 3.12 and 4.0 of the driver.

      In some situations this can result in 4.x producing an invalid query shape such as emitting a top level $not:

      package com.tour;
       
      import com.mongodb.ConnectionString;
      import com.mongodb.MongoClientSettings;
      import com.mongodb.client.*;
      import com.mongodb.client.model.Filters;
       
      import org.bson.BsonDocument;
      import org.bson.Document;
       
       
      public class Test12 {
       
          public static void main(final String[] args) {
       
            ConnectionString mongoURI = new ConnectionString("mongodb://localhost:27017/test");
            MongoClient mongoClient = MongoClients.create(mongoURI);
            MongoDatabase db = mongoClient.getDatabase("test");
            MongoCollection<Document> coll =  db.getCollection("foo");
            BsonDocument filters = Filters.and(
              Filters.and(
                      Filters.lt("field1", 100),
                      Filters.gt("field2", 100)),
              Filters.and(Filters.not(Filters.and(Filters.lt("value", 1))))).toBsonDocument(BsonDocument.class,
              MongoClientSettings.getDefaultCodecRegistry());
            System.out.println(filters);
       
            Document first = coll.find(filters).first();
       
            System.out.println(first);
          }
      }
      

      When testing the above in 3.12:

      <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongodb-driver-sync</artifactId>
        <version>3.12.7</version>
      </dependency>
      

      output: {"field1": {"$lt": 100}, "field2": {"$gt": 100}, "value": {"$not": {"$lt": 1}}}
      

      When testing the above in 4.0:

      <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongodb-driver-sync</artifactId>
        <version>4.0.0</version>
      </dependency>
      

      output:
      {"$and": [{"$and": [{"field1": {"$lt": 100}}, {"field2": {"$gt": 100}}]}, {"$and": [{"$not": {"$and": [{"value": {"$lt": 1}}]}}]}]}
      Exception in thread "main" com.mongodb.MongoQueryException: Query failed with error code 2 and error message 'unknown top level operator: $not' on server localhost:27017
              at com.mongodb.internal.operation.FindOperation$1.call(FindOperation.java:661)
              at com.mongodb.internal.operation.FindOperation$1.call(FindOperation.java:651)
              at com.mongodb.internal.operation.OperationHelper.withReadConnectionSource(OperationHelper.java:583)
              at com.mongodb.internal.operation.FindOperation.execute(FindOperation.java:651)
              at com.mongodb.internal.operation.FindOperation.execute(FindOperation.java:79)
              at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:178)
              at com.mongodb.client.internal.MongoIterableImpl.execute(MongoIterableImpl.java:135)
              at com.mongodb.client.internal.MongoIterableImpl.iterator(MongoIterableImpl.java:92)
              at com.tour.Test12.main(Test12.java:34)
      

      Attachments

        Issue Links

          Activity

            People

              jeff.yemin@mongodb.com Jeffrey Yemin
              alex.bevilacqua@mongodb.com Alex Bevilacqua
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: