MongoDB  2.6.0
Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | List of all members
mongo::Query Class Reference

Represents a Mongo query expression. More...

#include <dbclientinterface.h>

Public Member Functions

 Query (const BSONObj &b)
 
 Query (const string &json)
 
 Query (const char *json)
 
Querysort (const BSONObj &sortPattern)
 Add a sort (ORDER BY) criteria to the query expression. More...
 
Querysort (const string &field, int asc=1)
 Add a sort (ORDER BY) criteria to the query expression. More...
 
Queryhint (BSONObj keyPattern)
 Provide a hint to the query. More...
 
Queryhint (const string &jsonKeyPatt)
 
QueryminKey (const BSONObj &val)
 Provide min and/or max index limits for the query. More...
 
QuerymaxKey (const BSONObj &val)
 max is exclusive
 
Queryexplain ()
 Return explain information about execution of this query instead of the actual query results. More...
 
Querysnapshot ()
 Use snapshot mode for the query. More...
 
Querywhere (const string &jscode, BSONObj scope)
 Queries to the Mongo database support a $where parameter option which contains a javascript function that is evaluated to see whether objects being queried match its criteria. More...
 
Querywhere (const string &jscode)
 
QueryreadPref (ReadPreference pref, const BSONArray &tags)
 Sets the read preference for this query. More...
 
bool isComplex (bool *hasDollar=0) const
 
BSONObj getFilter () const
 
BSONObj getSort () const
 
BSONObj getHint () const
 
bool isExplain () const
 
string toString () const
 
 operator string () const
 

Static Public Member Functions

static bool isComplex (const BSONObj &obj, bool *hasDollar=0)
 
static bool hasReadPreference (const BSONObj &queryObj)
 

Public Attributes

BSONObj obj
 

Static Public Attributes

static const BSONField< BSONObjReadPrefField
 
static const BSONField
< std::string > 
ReadPrefModeField
 
static const BSONField< BSONArrayReadPrefTagsField
 

Detailed Description

Represents a Mongo query expression.

Typically one uses the QUERY(...) macro to construct a Query object. Examples: QUERY( "age" << 33 << "school" << "UCLA" ).sort("name") QUERY( "age" << GT << 30 << LT << 50 )

Member Function Documentation

Query & mongo::Query::explain ( )

Return explain information about execution of this query instead of the actual query results.

Normally it is easier to use the mongo shell to run db.find(...).explain().

bool mongo::Query::hasReadPreference ( const BSONObj queryObj)
static
Returns
true if the query object contains a read preference specification object.
Query & mongo::Query::hint ( BSONObj  keyPattern)

Provide a hint to the query.

Parameters
keyPatternKey pattern for the index to use. Example: hint("{ts:1}")
bool mongo::Query::isComplex ( bool *  hasDollar = 0) const
Returns
true if this query has an orderby, hint, or some other field
Query & mongo::Query::minKey ( const BSONObj val)

Provide min and/or max index limits for the query.

min <= x < max

Query & mongo::Query::readPref ( ReadPreference  pref,
const BSONArray tags 
)

Sets the read preference for this query.

Parameters
prefthe read preference mode for this query.
tagsthe set of tags to use for this query.
Query & mongo::Query::snapshot ( )

Use snapshot mode for the query.

Snapshot mode assures no duplicates are returned, or objects missed, which were present at both the start and end of the query's execution (if an object is new during the query, or deleted during the query, it may or may not be returned, even with snapshot mode).

Note that short query responses (less than 1MB) are always effectively snapshotted.

Currently, snapshot mode may not be used with sorting or explicit hints.

Query & mongo::Query::sort ( const BSONObj sortPattern)

Add a sort (ORDER BY) criteria to the query expression.

Parameters
sortPatternthe sort order template. For example to order by name ascending, time descending: { name : 1, ts : -1 } i.e. BSON( "name" << 1 << "ts" << -1 ) or fromjson(" name : 1, ts : -1 ")
Query& mongo::Query::sort ( const string &  field,
int  asc = 1 
)
inline

Add a sort (ORDER BY) criteria to the query expression.

This version of sort() assumes you want to sort on a single field.

Parameters
asc= 1 for ascending order asc = -1 for descending order
Query & mongo::Query::where ( const string &  jscode,
BSONObj  scope 
)

Queries to the Mongo database support a $where parameter option which contains a javascript function that is evaluated to see whether objects being queried match its criteria.

Use this helper to append such a function to a query object. Your query may also contain other traditional Mongo query terms.

Parameters
jscodeThe javascript function to evaluate against each potential object match. The function must return true for matched objects. Use the this variable to inspect the current object.
scopeSavedContext for the javascript object. List in a BSON object any variables you would like defined when the jscode executes. One can think of these as "bind variables".

Examples: conn.findOne("test.coll", Query("{a:3}").where("this.b == 2 || this.c == 3")); Query badBalance = Query().where("this.debits - this.credits < 0");


The documentation for this class was generated from the following files: