Skip to content

Commit

Permalink
SERVER-2896 check isNot when handling exists and missing field
Browse files Browse the repository at this point in the history
  • Loading branch information
astaple committed Apr 5, 2011
1 parent 74086c6 commit 78aeb2e
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
5 changes: 4 additions & 1 deletion db/matcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -708,6 +709,7 @@ namespace mongo {
}

if( p ) {
// Left portion of field name was not found.
return retMissing( em );
}
else {
Expand Down Expand Up @@ -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;
Expand Down
33 changes: 33 additions & 0 deletions jstests/exists5.js
Original file line number Diff line number Diff line change
@@ -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.