Upgrading to Java driver 4.11.1 breaks filters

Hi

I am managing a long overdue java driver upgrade from v3.12.7 to v4.11.1 and when using a filter in a MongoCollection.find(filter), I am now getting the error:

‘$and/$or/$nor must be a nonempty array’

I believe it’s linked to way I build my filters. They are a combination of AndQueries with OrQueries, so a typical query with just an AndQuery and no OrQuery would look like this:
And Filter{filters=[And Filter{filters=[Filter{fieldName='_id', value=some-id}]}, And Filter{filters=[]}]}

When I would convert this query to Json, I would get -

In v3.12.7:

result = bson.toBsonDocument(classOf[BsonDocument],
MongoClient.getDefaultCodecRegistry).toJson
 
result = {"_id": "some-id"}

In v4.11.1:

result = bson.toBsonDocument().toJson

result =  {"$and": [{"$and": [{"_id": "some-id"}]}, {"$and": []}]}

I believe the error comes from the additional ands, which is correct, given my bson filter. Is there a way to get my queries to ignore the nonempty arrays, similar to v3.12.7?

My current workaround is to replace the empty and section with Filters.empty() but it is legacy code so I am reluctant to change too much.

Hi @Steven_Truong , sorry to hear you are running into this issue. I suspect the breaking change was introduced in https://jira.mongodb.org/browse/JAVA-3338 and discussed in https://jira.mongodb.org/browse/JAVA-4006, where we decided against reverting it. Not many people have reported being affected by the change, but it looks like you are one of the unlucky ones.

At this point we don’t plan to revisit the decision to keep the new behavior and you will have to work around it in your application in order to upgrade to the 4.x driver series.

If this affects a lot of code, one option would be to copy the Filters.and implementation from the 3.x driver into your application, and use it instead of the 4.x driver implementation. That may limit the blast radius of possible regressions that you could introduce during the upgrade.

Regards,
Jeff

1 Like

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.