Navigation
This version of the manual is no longer supported.
  • Reference >
  • Query, Update, and Projection Operators Quick Reference

Query, Update, and Projection Operators Quick Reference

This document contains a list of all operators used with MongoDB in version 2.2. See Core MongoDB Operations (CRUD) for a higher level overview of the operations that use these operators, and Query, Update, Projection, and Aggregation Operators for a more condensed index of these operators.

Query Selectors

Comparison

Note

To express equal to (e.g. =) in the MongoDB query language, use JSON { key:value } structure. Consider the following prototype:

db.collection.find( { field: value } )

For example:

db.collection.find( { a: 42 } )

This query selects all the documents the where the a field holds a value of 42.

You may combine comparison operators to specify ranges:

db.collection.find( { field: { $gt: value1, $lt: value2 } } );

This statement returns all documents with field between value1 and value2.

Note

If the field contains an array and the query has multiple conditional operators, the field as a whole will match if either a single array element meets the conditions or a combination of array elements meet the conditions.

Example

Query a field that contains an array.

A collection students contains the following documents where the score field contains an array of values:

{ "_id" : 1, "score" : [ -1, 3 ] }
{ "_id" : 2, "score" : [ 1, 5 ] }
{ "_id" : 3, "score" : [ 5, 5 ] }

Then, the following query:

db.students.find( { score: { $gt: 0, $lt: 2 } } )

Will match the following documents:

{ "_id" : 1, "score" : [ -1, 3 ] }
{ "_id" : 2, "score" : [ 1, 5 ] }
  • In the document with _id equal to 1, the score: [ -1, 3 ] as a whole meets the specified conditions since the element -1 meets the $lt: 2 condition and the element 3 meets the $gt: 0 condition.
  • In the document with _id equal to 2, the score: [ 1, 5 ] as a whole meets the specified conditions since the element 1 meets both the $lt: 2 condition and the $gt: 0 condition.

Logical

Element

JavaScript

Geospatial

Use the following operators within the context of $near:

Use the following operators within the context of $within:

Projection