Reference Cards
for MongoDB
What is MongoDB?
MongoDB is an open-source, general
purpose database.
Instead of storing data in rows and columns as a relational
database does, MongoDB uses a document data model,
and stores a binary form of JSON documents called BSON.
Documents contain one or more fields, and each field contains
a value of a specific data type, including arrays and binary data.
Documents are stored in collections, and collections are stored
in databases. It may be helpful to think of documents as roughly
equivalent to rows in a relational database; fields as equivalent
to columns; and collections as tables. There are no fixed
schemas in MongoDB, so documents can vary in structure and
can be adapted dynamically.
MongoDB provides full index support, including secondary,
compound, and geospatial indexes. MongoDB also features a
rich query language, atomic update modifiers, text search, the
Aggregation Framework for analytics similar to SQL GROUP
BY operations, and MapReduce for complex in-place data
analysis.
Built-in replication with automated failover provides high
availability. Auto-sharding enables horizontal scaling for large
deployments. MongoDB also provides native, idiomatic drivers
for all popular programming languages and frameworks to
make development natural.
Queries
Queries
{a: 10}
Docs where a is 10, or an array containing
the value 10.
{a: 10, b: “hello”}
Docs where a is 10 and b is “hello”.
{a: {$gt: 10}}
Docs where a is greater than 10. Also
available: $lt (<), $gte (>=), $lte
(<=), and $ne (!=).
{a: {$in: [10, “hello”]}}
Docs where a is either 10 or “hello”.
{a: {$all: [10, “hello”]}}
Docs where a is an array containing both
10 and “hello”.
{"a.b": 10}
Docs where a is an embedded document
with b equal to 10.
{a: {$elemMatch: {b: 1, c: 2}}}
Docs where a is an array that contains an
element with both b equal to 1 and c equal
to 2.
{$or: [{a: 1}, {b: 2}]}
Docs where a is 1 or b is 2.
{a: /^m/}
Docs where a begins with the letter m.
One can also use the regex operator: {a:
{$regex: “^m”}}.
{a: {$mod: [10, 1]}}
Docs where a mod 10 is 1.
{a: {$type: 2}}
Docs where a is a string. See bsonspec.org
for more.
{ $text: { $search: “hello” } }
Docs that contain ”hello” on a text
search. Requires a text index.
Queries and What They Match
For More Information
http://docs.mongodb.org/manual/tutorial/query-documents/
http://docs.mongodb.org/manual/reference/operator/query/
Not Indexable Queries
The following queries cannot use indexes. These query forms should normally be
accompanied by at least one other query term which does use an index.
Queries and What They Match (continued)
{a: {$nin: [10, “hello”]}}
Docs where a is anything but 10 or
“hello”.
{a: {$size: 3}}
Docs where a is an array with exactly 3
elements.
{a: {$exists: true}}
Docs containing an a field.
{a: /foo.*bar/}
Docs where a matches the regular
expression foo.*bar.
{a: {$not: {$type: 2}}}
Docs where a is not a string. $not
negates any of the other query operators.
{a: {$near:
{$geometry:{
type: “Point”,
coordinates: [ -73.98,
40.75 ]
}}
} }
Docs sorted in order of nearest to farthest
from the given coordinates. For geospatial
queries one can also use $geoWithin
and $geoIntersects operators.
Queries