-
Type: Bug
-
Resolution: Done
-
Priority: Major - P3
-
None
-
Affects Version/s: 2.2.0
-
Component/s: Index Maintenance, Querying
-
Environment:MacOS 10.8.2
-
Query
-
OS X
A query $gt:0 matches all numbers greater than 0. Non numbers do not match. $not:{$gt:0} should match the opposite values of $gt:0, which means all numbers <=0 and all non numbers. I believe the matcher implements this behavior, but the FieldRange constructor employs type bracketing and only matches numbers <= 0.
Here is a test:
c = db.c; c.drop(); c.save( {} ); // Does not match. printjson( c.find( { a:{ $lt:0 } } ).toArray() ); printjson( c.find( { a:{ $lt:0 } } ).explain() ); // Does match. printjson( c.find( { a:{ $not:{ $gt:0 } } } ).toArray() ); printjson( c.find( { a:{ $not:{ $gt:0 } } } ).explain() ); c.ensureIndex( { a:1 } ); // Does not match. printjson( c.find( { a:{ $lt:0 } } ).toArray() ); // Includes numbers < 0. printjson( c.find( { a:{ $lt:0 } } ).explain() ); // Now does not match. printjson( c.find( { a:{ $not:{ $gt:0 } } } ).toArray() ); // Includes numbers < 0. But should also include non numbers. printjson( c.find( { a:{ $not:{ $gt:0 } } } ).explain() );
-------------------------------------------------
Without an index, a $not $gt query will return docs where the queried field is missing. Adding an index on that field causes the query to return only documents where the field is present. See example below.
$ ./mongo
MongoDB shell version: 2.2.0
connecting to: test
> db.version()
2.2.0
> db.foo.drop();
true
> db.foo.insert(
);
> db.foo.insert(
);
> db.foo.find({data: {$not: {$gt: 2}}});
> db.foo.ensureIndex(
{data: 1});
> db.foo.find({data: {$not: {$gt: 2}}});
>
- is duplicated by
-
SERVER-3596 type bracketing used for $not query indexing, but not matching
- Closed
-
SERVER-8026 When an index is present, $not operator doesn't return documents that don't contain this field
- Closed
- related to
-
SERVER-1212 tighter index bounds for negated regular expression
- Closed