summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorEliot Horowitz <eliot@10gen.com>2010-04-17 17:22:19 -0400
committerEliot Horowitz <eliot@10gen.com>2010-04-17 17:22:19 -0400
commite9c541820b499ea35cb0f5a07c43863be13fe4a7 (patch)
tree6359aefa27f367df43068715f2ddc43271043560
parent4dd94be57bf59a43d8088d9656bc3008c34e4b06 (diff)
downloadmongo-e9c541820b499ea35cb0f5a07c43863be13fe4a7.tar.gz
Query class supports $query
-rw-r--r--client/dbclient.cpp26
-rw-r--r--client/dbclient.h2
-rw-r--r--dbtests/querytests.cpp15
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;