[CSHARP-840] Query.Not does not handle $where correctly Created: 10/Oct/13  Updated: 02/Apr/15  Resolved: 19/Oct/13

Status: Closed
Project: C# Driver
Component/s: None
Affects Version/s: 1.8.3
Fix Version/s: 1.9

Type: Bug Priority: Major - P3
Reporter: Roman Kuzmin Assignee: Robert Stam
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Backwards Compatibility: Major Change

 Description   

There is a mistake in the method Not() of MongoDB.Driver.Builders.Query.

Line:
MongoDB.Driver\Builders\QueryBuilder.cs(513): return new QueryDocument(elementName, new BsonDocument("$ne", operatorValue));

Presumably there should be "$not" instead of "$ne".

Now a valid Where query { "$where" :

{ "$code" : "this.Length == null" }

} is converted by Not() into invalid { "$where" : { "$ne" :

{ "$code" : "this.Length == null" }

} } which fails with an error:

$where expression has an unexpected type (response:

{ "errmsg" : "exception: $where expression has an unexpected type", "code" : 15902, "ok" : 0.0 }

)

Driver version 1.8.3



 Comments   
Comment by auto [ 19/Oct/13 ]

Author:

{u'username': u'rstam', u'name': u'rstam', u'email': u'robert@10gen.com'}

Message: CSHARP-840: Refactor implementation of Query.Not from one complex method to several smaller ones. Change QueryBuilder unit tests so all tests test for the negated version of the query as well.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/42d6984a554629af5dd61ecaa062eab911cf3585

Comment by auto [ 19/Oct/13 ]

Author:

{u'username': u'rstam', u'name': u'Robert Stam', u'email': u'robert@10gen.com'}

Message: CSHARP-840: A more general fix for $operators and stricter checking for when equality testing is implied.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/11ebe3082d5db0fac998279746cce4d0cd5969b2

Comment by auto [ 19/Oct/13 ]

Author:

{u'username': u'rstam', u'name': u'Robert Stam', u'email': u'robert@10gen.com'}

Message: CSHARP-840: Handle $where correctly in Query.Not.
Branch: master
https://github.com/mongodb/mongo-csharp-driver/commit/f43533d1642981c1f54c701b3dad675f148c2178

Comment by Robert Stam [ 11/Oct/13 ]

In code review. Branch:

https://github.com/rstam/mongo-csharp-driver/tree/csharp840

Comment by Robert Stam [ 10/Oct/13 ]

I think the bug is that we assume that anything that doesn't match all the tests above that line must be an equality comparison, which as you have shown isn't always true.

When we fix this we will also be sure to be more robust in the face of any future query operators that are introduced.

Thanks for report this.

Comment by Roman Kuzmin [ 10/Oct/13 ]

Or, if "$ne" still makes sense for other cases, then a special case for negation of $where should be added.

Generated at Wed Feb 07 21:37:59 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.