Skip to content

Commit

Permalink
fix a.0.x for regex SERVER-799
Browse files Browse the repository at this point in the history
  • Loading branch information
erh committed Mar 22, 2010
1 parent fd76166 commit 4aea2b8
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 25 deletions.
73 changes: 48 additions & 25 deletions db/jsobj.cpp
Expand Up @@ -845,36 +845,59 @@ namespace mongo {
}

void BSONObj::getFieldsDotted(const char *name, BSONElementSet &ret ) const {
BSONElement e = getField( name );
if ( e.eoo() ) {
const char *p = strchr(name, '.');
if ( p ) {
string left(name, p-name);
BSONElement e = getField( left );
if ( e.type() == Array ) {
BSONObjIterator i( e.embeddedObject() );
while( i.moreWithEOO() ) {
BSONElement f = i.next();
if ( f.eoo() )
break;
BSONObjIterator i(*this);
while ( i.more() ){
BSONElement e = i.next();
FieldCompareResult cmp = compareDottedFieldNames( name , e.fieldName() );
switch ( cmp ){

case LEFT_BEFORE:
case RIGHT_BEFORE:
break;

case RIGHT_SUBFIELD:
assert(0);
break;

case LEFT_SUBFIELD: {
const char * next = name + strlen( e.fieldName() ) + 1;
bool allDigits = false;
if ( isdigit( *next ) ){
const char * temp = next + 1;
while ( isdigit( *temp ) )
temp++;
allDigits = *temp == '.';
}

if ( e.type() == Object || allDigits ){
e.embeddedObject().getFieldsDotted( next , ret );
}
else if ( e.type() == Array ){
BSONObjIterator j( e.embeddedObject() );
while ( j.more() ){
BSONElement f = j.next();
if ( f.type() == Object )
f.embeddedObject().getFieldsDotted(p+1, ret);
f.embeddedObject().getFieldsDotted( next , ret );
}
} else if ( e.type() == Object ) {
e.embeddedObject().getFieldsDotted(p+1, ret);
}
else {
// intentially left blank, this means no match
}
return;
}
} else {
if ( e.type() == Array ) {
BSONObjIterator i( e.embeddedObject() );
while( i.moreWithEOO() ) {
BSONElement f = i.next();
if ( f.eoo() )
break;
ret.insert( f );

case SAME: {
if ( e.type() == Array ){
BSONObjIterator j( e.embeddedObject() );
while ( j.more() )
ret.insert( j.next() );
}
} else {
ret.insert( e );
else {
ret.insert( e );
}
return;
}

}
}
}
Expand Down
25 changes: 25 additions & 0 deletions jstests/regex_embed1.js
@@ -0,0 +1,25 @@

t = db.regex_embed1

t.drop()

t.insert( { _id : 1 , a : [ { x : "abc" } , { x : "def" } ] } )
t.insert( { _id : 2 , a : [ { x : "ab" } , { x : "de" } ] } )
t.insert( { _id : 3 , a : [ { x : "ab" } , { x : "de" } , { x : "abc" } ] } )

function test( m ){
assert.eq( 3 , t.find().itcount() , m + "1" );
assert.eq( 2 , t.find( { "a.x" : "abc" } ).itcount() , m + "2" );
assert.eq( 2 , t.find( { "a.x" : /.*abc.*/ } ).itcount() , m + "3" );

assert.eq( 1 , t.find( { "a.0.x" : "abc" } ).itcount() , m + "4" );
assert.eq( 1 , t.find( { "a.0.x" : /abc/ } ).itcount() , m + "5" );
}

test( "A" );

t.ensureIndex( { "a.x" : 1 } )
test( "B" );



0 comments on commit 4aea2b8

Please sign in to comment.