mirror of
https://github.com/mongodb/mongo.git
synced 2024-11-29 16:47:28 +01:00
Add $size query option
This commit is contained in:
parent
835506af0a
commit
7ba4140d5c
@ -407,6 +407,8 @@ namespace mongo {
|
||||
}
|
||||
else if ( fn[1] == 'i' && fn[2] == 'n' && fn[3] == 0 )
|
||||
return JSMatcher::opIN;
|
||||
else if ( fn[1] == 's' && fn[2] == 'i' && fn[3] == 'z' && fn[4] == 'e' && fn[5] == 0 )
|
||||
return JSMatcher::opSIZE;
|
||||
}
|
||||
return JSMatcher::Equality;
|
||||
}
|
||||
|
@ -258,6 +258,14 @@ namespace mongo {
|
||||
addBasic(e, opIN); // e not actually used at the moment for $in
|
||||
ok = true;
|
||||
}
|
||||
else if ( fn[1] == 's' && fn[2] == 'i' && fn[3] == 'z' && fn[4] == 'e' ) {
|
||||
uassert( "$size must be a number", fe.isNumber() );
|
||||
BSONObjBuilder *b = new BSONObjBuilder();
|
||||
builders[nBuilders++] = b;
|
||||
b->appendAs(fe, e.fieldName());
|
||||
addBasic(b->done().firstElement(), opSIZE);
|
||||
ok = true;
|
||||
}
|
||||
else
|
||||
uassert("invalid $operator", false);
|
||||
}
|
||||
@ -289,6 +297,20 @@ namespace mongo {
|
||||
return c;
|
||||
}
|
||||
|
||||
if ( op == opSIZE ) {
|
||||
if ( l.type() != Array )
|
||||
return 0;
|
||||
int count = 0;
|
||||
BSONObjIterator i( l.embeddedObject() );
|
||||
while( i.more() ) {
|
||||
BSONElement e = i.next();
|
||||
if ( e.eoo() )
|
||||
break;
|
||||
++count;
|
||||
}
|
||||
return count == r.number();
|
||||
}
|
||||
|
||||
/* check LT, GTE, ... */
|
||||
if ( !( l.isNumber() && r.isNumber() ) && ( l.type() != r.type() ) )
|
||||
return false;
|
||||
|
@ -86,7 +86,8 @@ namespace mongo {
|
||||
GTE = 0x6,
|
||||
GT = 0x4,
|
||||
opIN = 0x8, // { x : { $in : [1,2,3] } }
|
||||
NE = 0x9
|
||||
NE = 0x9,
|
||||
opSIZE = 0x10
|
||||
};
|
||||
|
||||
static int opDirection(int op) {
|
||||
|
@ -64,6 +64,16 @@ namespace MatcherTests {
|
||||
}
|
||||
};
|
||||
|
||||
class Size {
|
||||
public:
|
||||
void run() {
|
||||
JSMatcher m( fromjson( "{a:{$size:4}}" ) );
|
||||
ASSERT( m.matches( fromjson( "{a:[1,2,3,4]}" ) ) );
|
||||
ASSERT( !m.matches( fromjson( "{a:[1,2,3]}" ) ) );
|
||||
ASSERT( !m.matches( fromjson( "{a:[1,2,3,'a','b']}" ) ) );
|
||||
}
|
||||
};
|
||||
|
||||
class All : public UnitTest::Suite {
|
||||
public:
|
||||
All() {
|
||||
@ -71,6 +81,7 @@ namespace MatcherTests {
|
||||
add< DoubleEqual >();
|
||||
add< MixedNumericEqual >();
|
||||
add< MixedNumericGt >();
|
||||
add< Size >();
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -383,6 +383,7 @@ namespace mongo {
|
||||
break;
|
||||
case JSMatcher::opIN:
|
||||
case JSMatcher::NE:
|
||||
case JSMatcher::opSIZE:
|
||||
massert("not implemented yet relevant()", false);
|
||||
case JSMatcher::Equality:
|
||||
goto normal;
|
||||
|
Loading…
Reference in New Issue
Block a user