Query Modifiers

We have number of meta operation in addition to the MongoDB Query Operators to modify the output or behaviour of a query.

snippet
db.collection.find( { <query> } )._addSpecial( <option> )
db.collection.find( { $query: { <query> }, <option> } )

Modifiers

$comment

The comment operator makes it possible to add a comment to a query in any context.

Syntax:

snippet
db.collection.find( { <query> } )._addSpecial( "$comment", <comment> )

$explain

The explain modifier provides details about the query plan. It returns a file that describes the process and indexes used to return the query. It may give useful insight when attempting to optimize a query.

Syntax:

snippet
db.example.find( { $query: {}, $explain: 1 } )

$hint

This operator is deprecated in the mongo shell now. The hint operator attaches the optimizer to use the declared index to fulfill the query. It is also used for testing query performance and indexing strategies.

Syntax:

snippet
db.users.find().hint( { age: 1 } )

$max

The max operator is deprecated in mongo shell since v3.2. It defines a max value to specify the exclusive upper bound for the given index in order to limit the results of find ().

Syntax:

snippet
db.example.find( { <query>  } ).max( { field1: <max value>, ... fieldN: <max valueN> } )

$maxTimeMS

It is also deprecated since v3.2. It defines a cumulative time in ms for processing operations on the cursor.

Syntax:

snippet
db.collection.find().maxTimeMS(100)

$min

The min operator is used to find a minimum value to declare the included lower bound for a specified index to constrain the results of find ().

Syntax:

snippet
db.collection.find( { <query> } ).min( { field1: <min value>, ... fieldN: <min valueN>} )

$orderby

The orderby operator arranges the results of a query in ascending or descending order.

Syntax:

snippet
db.collection.find().sort( { age: -1 } )

$query

It forcefully interprets an expression as a query using MongoDB.

Syntax:

snippet
db.collection.find( { $query: { age : 25 } } )
db.collection.find( { age : 25 } )

$returnKey

The return key returns the index fields for the results of the query. If returnkey operator is set to true then the returned documents will not contain any fields.

Syntax:

snippet
db.collection.find( { <query> } )._addSpecial( "$returnKey", true )
db.collection.find( { $query: { <query> }, $returnKey: true } )

$showDiskLoc

The showDiskLoc operator adds a field to the resultant documents. The value of the added diskLoc field is a document that contains the disk location details.

Syntax:

snippet
"$diskLoc": {
  "file": <int>,
  "offset": <int>
}

$natural

The natural operator is a special sort order operator that arranges the documents using the order of documents on disk using the cursor.hint ().

Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +