Skip to content

Commit

Permalink
fix compount key with array indexing SERVER-279
Browse files Browse the repository at this point in the history
  • Loading branch information
erh committed Oct 8, 2009
1 parent a0d3663 commit 3e3dda9
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 3 deletions.
17 changes: 15 additions & 2 deletions db/pdfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -689,11 +689,24 @@ namespace mongo {
} else {
// terminal array element to expand, so generate all keys
BSONObjIterator i( arrElt.embeddedObject() );
while( i.more() ) {
if ( i.more() ){
while( i.more() ) {
BSONObjBuilder b;
for( unsigned j = 0; j < fixed.size(); ++j ) {
if ( j == arrIdx )
b.appendAs( i.next(), "" );
else
b.appendAs( fixed[ j ], "" );
}
keys.insert( b.obj() );
}
}
else if ( fixed.size() > 1 ){
// x : [] - need to insert undefined
BSONObjBuilder b;
for( unsigned j = 0; j < fixed.size(); ++j ) {
if ( j == arrIdx )
b.appendAs( i.next(), "" );
b.appendUndefined( "" );
else
b.appendAs( fixed[ j ], "" );
}
Expand Down
59 changes: 59 additions & 0 deletions dbtests/namespacetests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,63 @@ namespace NamespaceTests {
return aDotB();
}
};

class EmptyArray : Base {
public:
void run(){
create();

BSONObjSetDefaultOrder keys;
id().getKeysFromObject( fromjson( "{a:[1,2]}" ), keys );
checkSize(2, keys );
keys.clear();

id().getKeysFromObject( fromjson( "{a:[1]}" ), keys );
checkSize(1, keys );
keys.clear();

id().getKeysFromObject( fromjson( "{a:null}" ), keys );
checkSize(1, keys );
keys.clear();

id().getKeysFromObject( fromjson( "{a:[]}" ), keys );
checkSize(1, keys );
keys.clear();
}
};

class MultiEmptyArray : Base {
public:
void run(){
create();

BSONObjSetDefaultOrder keys;
id().getKeysFromObject( fromjson( "{a:1,b:[1,2]}" ), keys );
checkSize(2, keys );
keys.clear();

id().getKeysFromObject( fromjson( "{a:1,b:[1]}" ), keys );
checkSize(1, keys );
keys.clear();

id().getKeysFromObject( fromjson( "{a:1,b:null}" ), keys );
cout << "YO : " << *(keys.begin()) << endl;
checkSize(1, keys );
keys.clear();

id().getKeysFromObject( fromjson( "{a:1,b:[]}" ), keys );
checkSize(1, keys );
cout << "YO : " << *(keys.begin()) << endl;
ASSERT_EQUALS( NumberDouble , keys.begin()->firstElement().type() );
keys.clear();
}

protected:
BSONObj key() const {
return aAndB();
}
};


} // namespace IndexDetailsTests

Expand Down Expand Up @@ -720,6 +777,8 @@ namespace NamespaceTests {
add< IndexDetailsTests::ParallelArraysComplex >();
add< IndexDetailsTests::AlternateMissing >();
add< IndexDetailsTests::MultiComplex >();
add< IndexDetailsTests::EmptyArray >();
add< IndexDetailsTests::MultiEmptyArray >();
add< IndexDetailsTests::MissingField >();
add< IndexDetailsTests::SubobjectMissing >();
add< IndexDetailsTests::CompoundMissing >();
Expand Down
2 changes: 1 addition & 1 deletion jstests/multi2.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ assert.eq( 1 , t.find( { x : 1 , a : null } ).count() , "B2" );

t.dropIndex( { x : 1 } );
t.ensureIndex( { x : 1 , a : 1 } );
//assert.eq( 3 , t.find( { x : 1 } ).count() , "C" ); // SERVER-279 / 146
assert.eq( 3 , t.find( { x : 1 } ).count() , "C" ); // SERVER-279
assert.eq( 4 , t.find().sort( { x : 1 , a : 1 } ).count() , "s2" );
assert.eq( 1 , t.find( { x : 1 , a : null } ).count() , "C2" );

Expand Down

0 comments on commit 3e3dda9

Please sign in to comment.