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

Query class supports $query

This commit is contained in:
Eliot Horowitz 2010-04-17 17:22:19 -04:00
parent 4dd94be57b
commit e9c541820b
3 changed files with 35 additions and 8 deletions

View File

@ -30,7 +30,7 @@ namespace mongo {
Query& Query::where(const string &jscode, BSONObj scope) {
/* use where() before sort() and hint() and explain(), else this will assert. */
assert( !obj.hasField("query") );
assert( ! isComplex() );
BSONObjBuilder b;
b.appendElements(obj);
b.appendWhere(jscode, scope);
@ -39,7 +39,7 @@ namespace mongo {
}
void Query::makeComplex() {
if ( obj.hasElement( "query" ) )
if ( isComplex() )
return;
BSONObjBuilder b;
b.append( "query", obj );
@ -76,14 +76,28 @@ namespace mongo {
return *this;
}
bool Query::isComplex() const{
return obj.hasElement( "query" );
bool Query::isComplex( bool * hasDollar ) const{
if ( obj.hasElement( "query" ) ){
if ( hasDollar )
hasDollar[0] = false;
return true;
}
if ( obj.hasElement( "$query" ) ){
if ( hasDollar )
hasDollar[0] = true;
return true;
}
return false;
}
BSONObj Query::getFilter() const {
if ( ! isComplex() )
bool hasDollar;
if ( ! isComplex( &hasDollar ) )
return obj;
return obj.getObjectField( "query" );
return obj.getObjectField( hasDollar ? "$query" : "query" );
}
BSONObj Query::getSort() const {
if ( ! isComplex() )

View File

@ -160,7 +160,7 @@ namespace mongo {
/**
* if this query has an orderby, hint, or some other field
*/
bool isComplex() const;
bool isComplex( bool * hasDollar = 0 ) const;
BSONObj getFilter() const;
BSONObj getSort() const;

View File

@ -1037,6 +1037,17 @@ namespace QueryTests {
};
};
namespace queryobjecttests {
class names1 {
public:
void run(){
ASSERT_EQUALS( BSON( "x" << 1 ) , QUERY( "query" << BSON( "x" << 1 ) ).getFilter() );
ASSERT_EQUALS( BSON( "x" << 1 ) , QUERY( "$query" << BSON( "x" << 1 ) ).getFilter() );
}
};
}
class All : public Suite {
public:
All() : Suite( "query" ) {
@ -1085,8 +1096,10 @@ namespace QueryTests {
add< HelperByIdTest >();
add< FindingStart >();
add< WhatsMyUri >();
add< parsedtests::basic1 >();
add< queryobjecttests::names1 >();
}
} myall;