MongoDB ReferenceCards
MongoDB ReferenceCards
for MongoDB
What is MongoDB?
MongoDB is an open-source, general
purpose database.
{a: {$near:
{$geometry:{
Docs sorted in order of nearest to farthest
type: “Point”,
from the given coordinates. For geospatial
coordinates: [ -73.98,
queries one can also use $geoWithin
40.75 ]
}} and $geoIntersects operators.
} }
Queries
Updates
Updates
Field Update Modifiers
{ $setOnInsert: { a: 1 } },
Sets field a to 1 in case of upsert
{ upsert: true }
operation.
{$push: {a: {$each: [1, 2]}}} Appends both 1 and 2 to the array a.
{$addToSet: {a: {$each: [1, Appends both 1 and 2 to the array a (if
2]}}} they don’t already exist).
{$pop: {a: 1}} Removes the last element from the array a.
{$pop: {a: -1}} Removes the first element from the array a.
Updates
Aggregation
Framework
Aggregation Framework
The aggregation pipeline is a framework for data aggregation modeled on the
concept of data processing pipelines. Documents enter a multi-stage pipeline that
transforms the documents into aggregated results. Pipeline stages appear in an array.
Documents pass through the stages in sequence. Structure an aggregation pipeline
using the following syntax:
db.collection.aggregate( [ { <stage> }, ... ] )
{a:1, b:”John”},
Groups documents by field
{$group: { _id: “$a”, {a:1, b:”Mary”}
a with new field names
names: {$addToSet: => {_id:1,
consisting of a set of b
“$b”}} } names:[“John”,
values.
“Mary”] }
Deconstructs array field a {a: [2,3,4]} =>
{$unwind: “$a”} into individual documents of {a:2}, {a:3},
each element. {a:4}
Limits the set of documents
{$limit: 10} to 10, passing the first 10
documents.
Aggregation Framework
Indexing
Indexing
Examples
db.products.createIndex(
{‘description’: ‘text’}, Creates text index on description key
{‘default_language’: using Spanish for stemming.
‘spanish’})
Creates ascending sparse index on
db.products.createIndex( { regions key. If regions is an array –
‘regions’: 1 }, {sparse:true}) e.g., regions: [‘EMEA’, ‘NA’,
‘LATAM’] – will create a multikey index.
db.stores.createIndex(
Creates a 2dsphere geospatial index on
{location: “2dsphere”})
location key.
Indexing
Administration
Indexing
Replication
Replication
What is a Majority?
If your set consists of...
1 server, 1 server is a majority.
2 servers, 2 servers are a majority.
3 servers, 2 servers are a majority.
4 servers, 3 servers are a majority.
...
Setup
To initialize a three-node replica set including one arbiter, start three mongod
instances, each using the --replSet flag followed by a name for the replica set.
For example:
mongod --replSet cluster-foo
Next, connect to one of the mongod instances and run the following:
rs.initiate()
rs.add(“host2:27017”)
rs.add(“host3:27017”, true)
Administration
Replication
Administration (continued)
rs.printSlaveReplicatio Prints a report of the status of the replica set from the
nInfo() perspective of the secondaries.
Replication
Sharding
Sharding
table collection
index index
row document
column field
partition shard
Queries and other operations in MongoDB are represented as documents passed
to find()and other methods. Below are examples of SQL statements and the
analogous statements in MongoDB JavaScript shell syntax.
SQL MongoDB
db.users.find({}, {name: 1,
SELECT name, age FROM users
age: 1, _id:0})
db.users.find({age: {$exists:
SELECT COUNT(AGE) FROM users
true}}).count()
db.users.update({name: “Bob”},
UPDATE users SET age = 33 WHERE
{$set: {age: 33}}, {multi:
name = ‘Bob’
true})
db.users.update({name: “Bob”},
UPDATE users SET age = age + 2
{$inc: {age: 2}}, {multi:
WHERE name = ‘Bob’
true})
db.users.find({age: 32}).ex-
plain()
EXPLAIN SELECT * FROM users
WHERE age = 32
(db.users.explain().find({age:
32}) for 3.0)
db.users.aggregate( [ {$group:
SELECT age, SUM(1) AS counter
{‘_id’: ‘$age’, counter:
FROM users GROUP BY age
{$sum:1}} } ])
db.users.aggregate([
SELECT age, SUM(1) AS counter {$match: {country: ‘US’} },
FROM users WHERE country = “US” {$group: {‘_id’: ‘$age’,
GROUP BY age counter: {$sum:1}} }
])
db.users.aggregate( [
SELECT age AS “how_old” FROM {$project: {“how_old”:
users “$age”}}
])
Support
Stack Overflow - stackoverflow.com/questions/tagged/mongodb
Google Group - groups.google.com/group/mongodb-user
Bug Tracking - jira.mongodb.org
Commercial Support - mongodb.com/support
Community
MongoDB User Groups (MUGs) - mongodb.com/user-groups
MongoDB Events - mongodb.com/events
Social
Twitter - @MongoDB, @MongoDB_Inc
Facebook - facebook.com/mongodb
LinkedIn - linkedin.com/groups/MongoDB-2340731
Contact
Contact MongoDB - mongodb.com/contact