[JAVA-2877] Query generated by Filters.not is not always valid Created: 31/May/18  Updated: 28/Oct/23  Resolved: 21/Nov/18

Status: Closed
Project: Java Driver
Component/s: Builders
Affects Version/s: 3.7.1
Fix Version/s: 3.9.1, 3.10.0

Type: Bug Priority: Major - P3
Reporter: Boris Petrov Assignee: Ross Lawley
Resolution: Fixed Votes: 0
Labels: newgrad
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Filters.and(
  Filters.not(Filters.eq("foo", new DBRef("1", "1"))),
  Filters.not(Filters.eq("foo.bar", new DBRef("1", "1"))))

This code generates the query:

{ "foo" : { "$not" : { "$ref" : "1", "$id" : "1" } }, "foo.bar" : { "$not" : { "$ref" : "1", "$id" : "1" } } }

Which after that complains with:

com.mongodb.MongoQueryException: Query failed with error code 2 and error message 'unknown operator: $ref' on server localhost:27017

When passed to the DB. Am I missing something or is this a bug?



 Comments   
Comment by Githook User [ 14/Jan/19 ]

Author:

{'username': 'rozza', 'email': 'ross.lawley@gmail.com', 'name': 'Ross Lawley'}

Message: Support DBRef documents in Filters.not

JAVA-2877
Branch: 3.10.x
https://github.com/mongodb/mongo-java-driver/commit/b50cce9f083f42f261d9d4d1e67fdd9c4c1e8d8b

Comment by Githook User [ 21/Nov/18 ]

Author:

{'name': 'Ross Lawley', 'email': 'ross.lawley@gmail.com', 'username': 'rozza'}

Message: Support DBRef documents in Filters.not

JAVA-2877
Branch: 3.9.x
https://github.com/mongodb/mongo-java-driver/commit/777faa17265147fd44a77b80895f76c12f41e226

Comment by Githook User [ 21/Nov/18 ]

Author:

{'name': 'Ross Lawley', 'email': 'ross.lawley@gmail.com', 'username': 'rozza'}

Message: Support DBRef documents in Filters.not

JAVA-2877
Branch: master
https://github.com/mongodb/mongo-java-driver/commit/c80f84f020ad9b95b9d42edf80e360f849eeae29

Comment by Jeffrey Yemin [ 31/May/18 ]

Alternatively, you can use the $ne operator:

Filters.and(
  Filters.ne("foo", new DBRef("1", "1")),
  Filters.ne("foo.bar", new DBRef("1", "1")))

Comment by Jeffrey Yemin [ 31/May/18 ]

Hi Boris,

Thanks for the report. This looks like a bug in the Filters#not method, where it's not inserting a $eq between the $not and the DBRef document.

For now you can work around it by using the Document class to construct the query:

 new Document()
                .append("foo", new Document("$not", new Document("$eq", new DBRef("1", "1"))))
                .append("foo.bar", new Document("$not", new Document("$eq", new DBRef("1", "1"))));

Generated at Thu Feb 08 08:58:16 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.