diff options
author | Eliot Horowitz <eliot@10gen.com> | 2010-04-17 17:22:19 -0400 |
---|---|---|
committer | Eliot Horowitz <eliot@10gen.com> | 2010-04-17 17:22:19 -0400 |
commit | e9c541820b499ea35cb0f5a07c43863be13fe4a7 (patch) | |
tree | 6359aefa27f367df43068715f2ddc43271043560 | |
parent | 4dd94be57bf59a43d8088d9656bc3008c34e4b06 (diff) | |
download | mongo-e9c541820b499ea35cb0f5a07c43863be13fe4a7.tar.gz |
Query class supports $query
-rw-r--r-- | client/dbclient.cpp | 26 | ||||
-rw-r--r-- | client/dbclient.h | 2 | ||||
-rw-r--r-- | dbtests/querytests.cpp | 15 |
3 files changed, 35 insertions, 8 deletions
diff --git a/client/dbclient.cpp b/client/dbclient.cpp index f965054d36d..4932fa4d29b 100644 --- a/client/dbclient.cpp +++ b/client/dbclient.cpp @@ -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() ) diff --git a/client/dbclient.h b/client/dbclient.h index 280a5b9e84d..e5649216e74 100644 --- a/client/dbclient.h +++ b/client/dbclient.h @@ -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; diff --git a/dbtests/querytests.cpp b/dbtests/querytests.cpp index 2f24894f002..1677f7c17da 100644 --- a/dbtests/querytests.cpp +++ b/dbtests/querytests.cpp @@ -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; |