[JAVA-5106] AndFilter merging filters into single $expr Created: 14/Aug/23  Updated: 27/Oct/23  Resolved: 31/Aug/23

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

Type: Question Priority: Major - P3
Reporter: Nitul Kukadia Assignee: Jeffrey Yemin
Resolution: Gone away Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Documentation Changes Summary:

1. What would you like to communicate to the user about this feature?
2. Would you like the user to see examples of the syntax and/or executable code and its output?
3. Which versions of the driver/connector does this apply to?


 Description   

Summary

We have multiple  filters with $expr in a list, and we are passing this list in to method
AggregateIterable<TDocument> aggregate(List<? extends Bson> pipeline); of class MongoCollection.  
 
If we have and filter in it and if you see com.mongodb.client.model.Filters.AndFilter it will l combine all the condition into one condition.
 
Value passed as :

 

{"$expr": {"$eq": [ {"$strcasecmp": ["$CreatedBy", "tenant21"]}, 0] }}
 
{"$expr": {"$in": [ {"$toLower": "$a"}, ["a"]] }}

 
Command executed:

{"$expr": {"$eq": [ {"$strcasecmp": ["$CreatedBy", "tenant21"]}, 0], "$in": [{"$toLower": "$a"}, ["a"]]}}

If you see above it will combine two $expr into single $expr and while executing it will fail saying, $expr can only contain single object.



 Comments   
Comment by Nitul Kukadia [ 31/Aug/23 ]

Can not see mongodb-driver-core dependency in the document, you can close this issue.

Comment by Jeffrey Yemin [ 29/Aug/23 ]

If you start from scratch and follow the quick start you will end up with a working application (mongodb-driver-sync depends on mongodb-driver-core, which has the classes that you're not finding in your setup)

Comment by Nitul Kukadia [ 29/Aug/23 ]

You can ignore that, if you see this class https://github.com/mongodb/mongo-java-driver/blob/master/driver-sync/src/main/com/mongodb/client/internal/MongoClientImpl.java is having import of  com.mongodb.internal.diagnostics.logging.Loggers;

 

Now, Can you please check which maven dependency has definition of the com.mongodb.internal.diagnostics.logging.Loggers class which starts as com.mongodb.internal only.

so that I can include that dependency in my application.I tried to search but can not find any.

Comment by Jeffrey Yemin [ 28/Aug/23 ]

You must have a mix of versions in your class path.

Comment by Nitul Kukadia [ 28/Aug/23 ]

With 4.10.x version this import com.mongodb.internal.diagnostics.logging.Loggers; class is not resolved in MongoClientImpl.java class

Comment by Jeffrey Yemin [ 25/Aug/23 ]

This code:

        List<Bson> filtersList = new ArrayList<>();
        filtersList.add(
                Document.parse("{$expr:{$eq:[{$strcasecmp:[\"$CreatedBy\", \"tenant21\"]}, 0]}}"));
        filtersList.add(
                Document.parse("{$expr: {$in: [ {$toLower: \"$a\"}, [\"a\"]] }}"));
        Bson andFilter = Filters.and(filtersList);
        String json = andFilter.toBsonDocument().toJson(JsonWriterSettings.builder().indent(true).build());
        System.out.println(json);

outputs

{
  "$and": [
    {
      "$expr": {
        "$eq": [
          {
            "$strcasecmp": [
              "$CreatedBy",
              "tenant21"
            ]
          },
          0
        ]
      }
    },
    {
      "$expr": {
        "$in": [
          {
            "$toLower": "$a"
          },
          [
            "a"
          ]
        ]
      }
    }
  ]
}

on latest (4.10). Is this what you're expecting? Are you sure you're using a recent Java driver release in your tests? A fix for JAVA-3338, included in the 4.0 release of the driver, is the last change I'm aware of in the behavior of Filters.and.

Comment by Nitul Kukadia [ 24/Aug/23 ]

 

List<Bson> aggregateList = new ArrayList<>();
 
List<Bson> filtersList = new ArrayList<>(); 
filtersList.add(
    Document.parse("{$expr:{$eq:[{$strcasecmp:[\"$CreatedBy\", \"tenant21\"]}, 0]}}
")
);
filtersList.add(
    Document.parse("{$in:{$eq:[{$toLower::\"$a\"},a]}}}
")
);
 
 
MongoCollection<Document> collection = mongoClient.getDatabase("someDatabase").getCollection("somecollection");
aggregateList.add(Aggregates.match(Filters.and(filtersList)));
 
 
List<Document> result = collection.aggregate(aggregateList).into(new ArrayList<Document>());

You can also do get json from Aggregates.match(Filters.and(filtersList)) you will see the gneated wrong json command.

Comment by PM Bot [ 24/Aug/23 ]

Hi nitulkukadia! JAVA-5106 is awaiting your response.

If this is still an issue for you, please open Jira to review the latest status and provide your feedback. Thanks!

Comment by Jeffrey Yemin [ 16/Aug/23 ]

OK, will await your response.

Comment by Nitul Kukadia [ 14/Aug/23 ]

I think you can check latest driver only. And server also latest only. Try to execute aggregate pipeline which contains AndFilter with two conditions. Will provide asap.

Comment by Jeffrey Yemin [ 14/Aug/23 ]

Hi nitulkukadia , please provide the following so we may assist you further:

  • a code snippet that reproduces the error
  • the Java driver version
  • the server version
Comment by PM Bot [ 14/Aug/23 ]

Hi nitulkukadia, thank you for reporting this issue! The team will look into it and get back to you soon.

Generated at Thu Feb 08 09:03:45 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.