MongoDB Reference Card
MongoDB Reference Card
for MongoDB
What is MongoDB?
MongoDB is an open-source, general
purpose database.
Instead of storing data in rows and columns as one would with
a relational database, 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
Queries and What They Match
{a: 10}
{"a.b": 10}
Docs where a is 1 or b is 2.
{a: /^m/}
{a: /foo.*bar/}
Updates
Updates
Field Update Modifiers
Increments a by 2.
{$max: { a: 10 } }
{$mul: { a: 2 } }
{$rename: { a: b} }
Renames field a to b.
{ $setOnInsert: { a: 1 } },
{ upsert: true }
{$currentDate: { a: { $type:
date} } }
{ $bit: { a: { and: 7 } } }
1000
0100
------1100
Supports and|xor|or bitwise operators.
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> }, ... ] )
{$match: { a: 10 }}
Similar to find()
{$project: { a: 1,
_id:0}}
Similar to find()
projection
{$project: {
"$a" }}
{a:1} =>
{new_a:1}
{$project: { stats:
{
value: $a,
fraction: {$divide: [$a, $b]}
}
} }
{a:hello},
{a:goodbye},
{a:hello} =>
{_id:hello,
count:2}, {_
id:goodbye,
count:1}
new_a:
{$group: {
_id: $a,
count:{$sum:1}
} }
{a:1, b:John},
{a:1, b:Mary}
=> {_id:1,
names:[John,
Mary] }
{$unwind: $a}
{$limit: 10}
{$sort: {a:1}}
{$skip: 10}
{$out: myResults}
Aggregation Framework
Indexing
Indexing
Index Creation Syntax
db.coll.createIndex(<key_pattern>, <options>)
Creates an index on collection coll with given key pattern and options.
{a:1}
{a:1, b:-1}
{a.b: 1}
{a: text}
{a: 2dsphere}
{a: hashed}
Index options
{unique: true}
{background: true}
{name: foo}
{sparse: true}
{expireAfterSeconds:360}
{default_language:
portuguese}
Examples
db.products.createIndex(
{supplier:1}, {unique:true})
db.products.createIndex(
{description: text,
{default_language:
spanish})
db.products.createIndex( {
regions: 1 }, {sparse:true})
db.stores.createIndex(
{location: 2dsphere})
Administration
db.products.getIndexes()
db.products.reIndex()
db.products.dropIndex({x: 1, y:
-1})
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)
rs.add() can also accept a document parameter, such as rs.add({_id: 4,
host: host4:27017}). The document can contain the following options:
priority: n
votes: n
slaveDelay: n
arbiterOnly: true
Setup (continued)
hidden: true
tags: [...]
Administration
rs.initiate()
rs.add(host:port)
Adds a member.
rs.addArb(host:port)
Adds an arbiter.
rs.remove(host:port)
Removes a member.
rs.status()
rs.conf()
rs.reconfig(newConfig)
rs.isMaster()
rs.stepDown(n)
Replication
Administration (continued)
rs.freeze(n)
rs.printSlaveReplicatio
nInfo()
Replication
Sharding
Sharding
sh.enableSharding( products)
sh.shardCollection( products.
catalog, { sku:1, brand:1})
sh.status()
sh.addShard( REPLICA1/
host:27017)
Mapping SQL to
MongoDB
Oracle Executable
MongoDB Executable
mysqld
oracle
mongod
mysql
sqlplus
mongo
SQL Term
MongoDB Term
database (schema)
database
table
collection
index
index
row
document
column
field
joining
partition
shard
SQL
MongoDB
db.createCollection(users)
db.users.insert({name: Bob,
age: 32})
db.users.find()
db.users.find({}, {name: 1,
age: 1, _id:0})
db.users.find({age: {$lte:
33}})
db.users.find({$or:[{age:33},
{name:Bob}]})
db.users.find({age: 33}).
sort({name: 1})
db.users.find().sort({name:
-1})
db.users.find({name: /Joe/})
SQL
MongoDB
db.users.find({name: /^Joe/})
db.users.find().skip(20).limit(10)
db.users.findOne()
db.users.distinct(name)
db.users.count()
db.users.find({age: {$gt:
30}}).count()
db.users.find({age: {$exists:
true}}).count()
db.users.update({name: Bob},
{$set: {age: 33}}, {multi:
true})
db.users.update({name: Bob},
{$inc: {age: 2}}, {multi:
true})
db.users.remove({name: Bob})
db.users.createIndex({name: 1})
db.users.createIndex({name: 1,
age: -1})
db.users.find({age: 32}).explain()
(db.users.explain().find({age:
32}) for 3.0)
db.users.aggregate( [ {$group:
{_id: $age, counter:
{$sum:1}} } ])
SQL
MongoDB
db.users.aggregate([
{$match: {country: US} },
{$group: {_id: $age,
counter: {$sum:1}} }
])
db.users.aggregate( [
{$project: {how_old:
$age}}
])
Resources
Learn
Downloads - mongodb.org/downloads
Enterprise Advanced - mongodb.com/enterprise
MongoDB Manual - docs.mongodb.org
Free Online Education - university.mongodb.com
Presentations - mongodb.com/presentations
In-person Training - university.mongodb.com/training
Support
Stack Overflow - stackoverflow.com/questions/tagged/mongodb
Google Group - groups.google.com/group/mongodb-user
Bug Tracking - jira.mongodb.org
MongoDB Management Service - mms.mongodb.com
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