Skip to content

Commit

Permalink
fix exists with index SERVER-708
Browse files Browse the repository at this point in the history
  • Loading branch information
erh committed Mar 6, 2010
1 parent 02d6682 commit da4507b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
13 changes: 11 additions & 2 deletions db/matcher.cpp
Expand Up @@ -139,16 +139,18 @@ namespace mongo {
{
_needRecord = ! (
_docMatcher.keyMatch() &&
_keyMatcher.jsobj.nFields() == _docMatcher.jsobj.nFields()
_keyMatcher.jsobj.nFields() == _docMatcher.jsobj.nFields() &&
! _keyMatcher.hasType( BSONObj::opEXISTS )
);

}

bool CoveredIndexMatcher::matches(const BSONObj &key, const DiskLoc &recLoc , MatchDetails * details ) {
if ( details )
details->reset();

if ( _keyMatcher.keyMatch() ) {
if ( !_keyMatcher.matches(key) ) {
if ( !_keyMatcher.matches(key, details ) ){
return false;
}
}
Expand Down Expand Up @@ -710,6 +712,13 @@ namespace mongo {
return true;
}

bool Matcher::hasType( BSONObj::MatchType type ) const {
for ( unsigned i=0; i<basics.size() ; i++ )
if ( basics[i].compareOp == type )
return true;
return false;
}

struct JSObj1 js1;

#pragma pack(1)
Expand Down
1 change: 1 addition & 0 deletions db/matcher.h
Expand Up @@ -140,6 +140,7 @@ namespace mongo {

bool atomic() const { return _atomic; }

bool hasType( BSONObj::MatchType type ) const;
private:
void addBasic(const BSONElement &e, int c, bool isNot) {
// TODO May want to selectively ignore these element types based on op type.
Expand Down
14 changes: 14 additions & 0 deletions jstests/exists2.js
@@ -0,0 +1,14 @@

t = db.exists2;
t.drop();

t.save( { a : 1 , b : 1 } )
t.save( { a : 1 , b : 1 , c : 1 } )

assert.eq( 2 , t.find().itcount() , "A1" );
assert.eq( 2 , t.find( { a : 1 , b : 1 } ).itcount() , "A2" );
assert.eq( 1 , t.find( { a : 1 , b : 1 , c : { "$exists" : true } } ).itcount() , "A3" );

t.ensureIndex( { a : 1 , b : 1 , c : 1 } )
assert.eq( 1 , t.find( { a : 1 , b : 1 , c : { "$exists" : true } } ).itcount() , "B1" );

0 comments on commit da4507b

Please sign in to comment.