Permalink
Browse files

SERVER-2896 check isNot when handling exists and missing field

  • Loading branch information...
1 parent 74086c6 commit 78aeb2e1120766785cb85517735e6c1dd45c1851 @astaple astaple committed Apr 4, 2011
Showing with 37 additions and 1 deletion.
  1. +4 −1 db/matcher.cpp
  2. +33 −0 jstests/exists5.js
View
@@ -543,7 +543,7 @@ namespace mongo {
int retMissing( const ElementMatcher &bm ) {
if ( bm.compareOp != BSONObj::opEXISTS )
return 0;
- return bm.toMatch.boolean() ? -1 : 1;
+ return ( bm.toMatch.boolean() ^ bm.isNot ) ? -1 : 1;
}
/* Check if a particular field matches.
@@ -677,6 +677,7 @@ namespace mongo {
}
}
+ // An array was encountered while scanning for components of the field name.
if ( isArr ) {
DEBUGMATCHER( "\t\t isArr 1 : obj : " << obj );
BSONObjIterator ai(obj);
@@ -708,6 +709,7 @@ namespace mongo {
}
if( p ) {
+ // Left portion of field name was not found.
return retMissing( em );
}
else {
@@ -764,6 +766,7 @@ namespace mongo {
}
else if ( e.eoo() ) {
// 0 indicates "missing element"
+ // opEXISTS case already handled above, so retMissing() is unnecessary.
return 0;
}
return -1;
View
@@ -0,0 +1,33 @@
+// Test some $not/$exists cases.
+
+t = db.jstests_exists5;
+t.drop();
+
+t.save( {a:1} );
+assert.eq( 1, t.count( {'a.b':{$exists:false}} ) );
+assert.eq( 1, t.count( {'a.b':{$not:{$exists:true}}} ) );
+assert.eq( 1, t.count( {'c.d':{$not:{$exists:true}}} ) );
+assert.eq( 0, t.count( {'a.b':{$exists:true}} ) );
+assert.eq( 0, t.count( {'a.b':{$not:{$exists:false}}} ) );
+assert.eq( 0, t.count( {'c.d':{$not:{$exists:false}}} ) );
+
+t.drop();
+t.save( {a:{b:1}} );
+assert.eq( 1, t.count( {'a.b':{$exists:true}} ) );
+assert.eq( 1, t.count( {'a.b':{$not:{$exists:false}}} ) );
+assert.eq( 0, t.count( {'a.b':{$exists:false}} ) );
+assert.eq( 0, t.count( {'a.b':{$not:{$exists:true}}} ) );
+
+t.drop();
+t.save( {a:[1]} );
+assert.eq( 1, t.count( {'a.b':{$exists:false}} ) );
+assert.eq( 1, t.count( {'a.b':{$not:{$exists:true}}} ) );
+assert.eq( 0, t.count( {'a.b':{$exists:true}} ) );
+assert.eq( 0, t.count( {'a.b':{$not:{$exists:false}}} ) );
+
+t.drop();
+t.save( {a:[{b:1}]} );
+assert.eq( 1, t.count( {'a.b':{$exists:true}} ) );
+assert.eq( 1, t.count( {'a.b':{$not:{$exists:false}}} ) );
+assert.eq( 0, t.count( {'a.b':{$exists:false}} ) );
+assert.eq( 0, t.count( {'a.b':{$not:{$exists:true}}} ) );

0 comments on commit 78aeb2e

Please sign in to comment.