0
0
mirror of https://github.com/mongodb/mongo.git synced 2024-11-30 00:56:44 +01:00

SERVER-1004 $or with other top level fields

This commit is contained in:
Aaron 2010-04-13 15:17:34 -07:00
parent 41d65a81f9
commit 9ff7914250
4 changed files with 95 additions and 20 deletions

View File

@ -293,7 +293,7 @@ namespace mongo {
/* _jsobj - the query pattern
*/
Matcher::Matcher(const BSONObj &_jsobj, const BSONObj &constrainIndexKey) :
Matcher::Matcher(const BSONObj &_jsobj, const BSONObj &constrainIndexKey, bool subMatcher) :
where(0), jsobj(_jsobj), haveSize(), all(), hasArray(0), haveNeg(), _atomic(false), nRegex(0) {
BSONObjIterator i(jsobj);
@ -302,12 +302,14 @@ namespace mongo {
const char *ef = e.fieldName();
if ( ef[1] == 'o' && ef[2] == 'r' && ef[3] == 0 ) {
uassert( 13088, "recursive $or not allowed", !subMatcher );
uassert( 13086, "$or must be a nonempty array", e.type() == Array && e.embeddedObject().nFields() > 0 );
BSONObjIterator j( e.embeddedObject() );
while( j.more() ) {
BSONElement f = j.next();
uassert( 13087, "$or match element must be an object", f.type() == Object );
_orMatchers.push_back( shared_ptr< Matcher >( new Matcher( f.embeddedObject(), constrainIndexKey ) ) );
// until SERVER-109 this is never a covered index match, so don't constrain index key for $or matchers
_orMatchers.push_back( shared_ptr< Matcher >( new Matcher( f.embeddedObject(), BSONObj(), true ) ) );
}
break;
}
@ -716,21 +718,6 @@ namespace mongo {
/* assuming there is usually only one thing to match. if more this
could be slow sometimes. */
// for now $or must be the only top level field if present
if ( _orMatchers.size() > 0 ) {
for( vector< shared_ptr< Matcher > >::const_iterator i = _orMatchers.begin();
i != _orMatchers.end(); ++i ) {
if( details ) {
// just to be safe - may not be strictly necessary.
details->reset();
}
if ( (*i)->matches( jsobj, details ) ) {
return true;
}
}
return false;
}
// check normal non-regex cases:
for ( unsigned i = 0; i < basics.size(); i++ ) {
ElementMatcher& bm = basics[i];
@ -773,6 +760,23 @@ namespace mongo {
return false;
}
if ( _orMatchers.size() > 0 ) {
bool match = false;
for( vector< shared_ptr< Matcher > >::const_iterator i = _orMatchers.begin();
i != _orMatchers.end(); ++i ) {
// SERVER-205 don't submit details - we don't want to track field
// matched within $or, and at this point we've already loaded the
// whole document
if ( (*i)->matches( jsobj ) ) {
match = true;
break;
}
}
if ( !match ) {
return false;
}
}
if ( where ) {
if ( where->func == 0 ) {
uassert( 10070 , "$where compile error", false);

View File

@ -136,13 +136,14 @@ namespace mongo {
// Only specify constrainIndexKey if matches() will be called with
// index keys having empty string field names.
Matcher(const BSONObj &pattern, const BSONObj &constrainIndexKey = BSONObj());
Matcher(const BSONObj &pattern, const BSONObj &constrainIndexKey = BSONObj(), bool subMatcher = false);
~Matcher();
bool matches(const BSONObj& j, MatchDetails * details = 0 );
bool keyMatch() const { return !all && !haveSize && !hasArray && !haveNeg; }
// until SERVER-109 $or is opaque to indexes
bool keyMatch() const { return !all && !haveSize && !hasArray && !haveNeg && _orMatchers.size() == 0; }
bool atomic() const { return _atomic; }

68
jstests/or2.js Normal file
View File

@ -0,0 +1,68 @@
t = db.jstests_or2;
t.drop();
checkArrs = function( a, b, m ) {
assert.eq( a.length, b.length, m );
aStr = [];
bStr = [];
a.forEach( function( x ) { aStr.push( tojson( x ) ); } );
b.forEach( function( x ) { bStr.push( tojson( x ) ); } );
for ( i in aStr ) {
assert( -1 != bStr.indexOf( aStr[ i ] ), m );
}
}
doTest = function( index ) {
if ( index == null ) {
index = true;
}
t.save( {_id:0,x:0,a:1} );
t.save( {_id:1,x:0,a:2} );
t.save( {_id:2,x:0,b:1} );
t.save( {_id:3,x:0,b:2} );
t.save( {_id:4,x:1,a:1,b:1} );
t.save( {_id:5,x:1,a:1,b:2} );
t.save( {_id:6,x:1,a:2,b:1} );
t.save( {_id:7,x:1,a:2,b:2} );
assert.throws( function() { t.find( { x:0,$or:"a" } ).toArray(); } );
assert.throws( function() { t.find( { x:0,$or:[] } ).toArray(); } );
assert.throws( function() { t.find( { x:0,$or:[ "a" ] } ).toArray(); } );
assert.throws( function() { t.find( { x:0,$or:[ {x:0,$or:[{x:0}]} ] } ).toArray(); } );
a1 = t.find( { x:0, $or: [ { a : 1 } ] } ).toArray();
checkArrs( [ { _id:0, x:0, a:1 } ], a1 );
if ( index ) {
assert( t.find( { x:0,$or: [ { a : 1 } ] } ).explain().cursor.match( /Btree/ ) );
}
a1b2 = t.find( { x:1, $or: [ { a : 1 }, { b : 2 } ] } ).toArray();
checkArrs( [ { _id:4, x:1, a:1, b:1 }, { _id:5, x:1, a:1, b:2 }, { _id:7, x:1, a:2, b:2 } ], a1b2 );
if ( index ) {
assert( t.find( { x:0,$or: [ { a : 1 } ] } ).explain().cursor.match( /Btree/ ) );
}
t.drop();
obj = {_id:0,x:10,a:[1,2,3]};
t.save( obj );
t.update( {x:10,$or:[ {a:2} ]}, {$set:{'a.$':100}} );
assert.eq( obj, t.findOne() ); // no change
}
doTest( false );
t.ensureIndex( { x:1 } );
doTest();
t.drop();
t.ensureIndex( { x:1,a:1 } );
doTest();
t.drop();
t.ensureIndex( {x:1,b:1} );
doTest();
t.drop();
t.ensureIndex( {x:1,a:1,b:1} );
doTest();

View File

@ -539,6 +539,7 @@
93E727090F4B5B5B004F9B5D /* shardkey.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = shardkey.cpp; sourceTree = "<group>"; };
93E7270A0F4B5B5B004F9B5D /* shardkey.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = shardkey.h; sourceTree = "<group>"; };
93E8A4381173E6480025F7F8 /* or1.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = or1.js; sourceTree = "<group>"; };
93E8A4D01174EEAF0025F7F8 /* or2.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = or2.js; sourceTree = "<group>"; };
93F0957010E165E50053380C /* basic.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = basic.js; sourceTree = "<group>"; };
93F095CC10E16FF70053380C /* shellfork.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = shellfork.js; sourceTree = "<group>"; };
C6859E8B029090EE04C91782 /* mongo.1 */ = {isa = PBXFileReference; lastKnownFileType = text.man; path = mongo.1; sourceTree = "<group>"; };
@ -780,6 +781,7 @@
934BEB9A10DFFA9600178102 /* jstests */ = {
isa = PBXGroup;
children = (
93E8A4D01174EEAF0025F7F8 /* or2.js */,
93E8A4381173E6480025F7F8 /* or1.js */,
930750A8114EFB9900272A70 /* update_addToSet.js */,
930750A9114EFB9900272A70 /* update_arraymatch1.js */,