5. MongoDB
MongoDB (from "humongous") is an
open-source document database, and the
leading NoSQL database.
http://www.mongodb.org
6. Intro
MongoDB is a document database that provides high performance, high availability, and easy scalability.
• Document Database
◦ Documents (objects) map nicely to programming language data types.
◦ Embedded documents and arrays reduce need for joins.
◦ Dynamic schema makes polymorphism easier.
• High Performance
◦ Embedding makes reads and writes fast.
◦ Indexes can include keys from embedded documents and arrays.
◦ Optional streaming writes (no acknowledgments).
• High Availability
◦ Replicated servers with automatic master failover.
• Easy Scalability
◦ Automatic sharding distributes collection data across machines.
◦ Eventually-consistent reads can be distributed over replicated servers.
http://www.mongodb.org/about/introduction/
10. Full Index Support
Type Description
Single Field
A single field index only includes data from a single field of the
documents in a collection. MongoDB supports single field indexes on
fields at the top level of a document and on fields in sub-documents.
Compound
A compound index includes more than one field of the documents in a
collection.
Multikey
A multikey index references an array and records a match if a query
includes any value in the array.
Geospatial
Geospatial indexes support location-based searches on data that is
stored as either GeoJSON objects or legacy coordinate pairs.
Text Text indexes supports search of string content in documents.
Hashed
Hashed indexes maintain entries with hashes of the values of the
indexed field.
11. Replication
Replica sets provide redundancy and high availability,
and are the basis for all production deployments.
20. mongo
mongo is an interactive JavaScript shell interface to
MongoDB, which provides a powerful interface for
systems administrators as well as a way for
developers to test queries and operations directly
with the database. mongo also provides a fully
functional JavaScript environment for use with a
MongoDB.
http://docs.mongodb.org/manual/reference/program/mongo/
22. mongod
mongod is the primary daemon process for the
MongoDB system. It handles data requests, manages
data format, and performs background management
operations.
http://docs.mongodb.org/manual/reference/program/mongod/
23. mongos
mongos for “MongoDB Shard,” is a routing service
for MongoDB shard configurations that processes
queries from the application layer, and determines the
location of this data in the sharded cluster, in order to
complete these operations. From the perspective of
the application, a mongos instance behaves
identically to any other MongoDB instance.
http://docs.mongodb.org/manual/reference/program/mongos/
24. mongodump
mongodump is a utility for creating a binary export of
the contents of a database. Consider using this utility
as part an effective backup strategy. Use
mongodump in conjunction with mongorestore to
restore databases.
mongodump can read data from either mongod or
mongos instances, in addition to reading directly from
MongoDB data files without an active mongod.
http://docs.mongodb.org/manual/reference/program/mongodump/
25. bsondump
The bsondump converts BSON files into human-readable
formats, including JSON. For example,
bsondump is useful for reading the output files
generated by mongodump.
http://docs.mongodb.org/manual/reference/program/bsondump/
26. mongorestore
The mongorestore program writes data from a binary
database dump created by mongodump to a
MongoDB instance. mongorestore can create a new
database or add data to an existing database.
mongorestore can write data to either mongod or
mongos instances, in addition to writing directly to
MongoDB data files without an active mongod.
http://docs.mongodb.org/manual/reference/program/mongorestore/
27. mongoexport
mongoexport is a utility that produces a JSON or
CSV export of data stored in a MongoDB instance.
See the Import and Export MongoDB Data document
for a more in depth usage overview, and the
mongoimport document for more information
regarding the mongoimport utility, which provides the
inverse “importing” capability.
http://docs.mongodb.org/manual/reference/program/mongoexport/
28. mongoimport
The mongoimport tool provides a route to import
content from a JSON, CSV, or TSV export created by
mongoexport, or potentially, another third-party export
tool. See the Import and Export MongoDB Data
document for a more in depth usage overview, and the
mongoexport document for more information regarding
mongoexport, which provides the inverse “exporting”
capability.
http://docs.mongodb.org/manual/reference/program/mongoimport/
29. mongofiles
The mongofiles utility makes it possible to manipulate
files stored in your MongoDB instance in GridFS
objects from the command line. It is particularly
useful as it provides an interface between objects
stored in your file system and GridFS.
http://docs.mongodb.org/manual/reference/program/mongofiles/
30. mongooplog
mongooplog is a simple tool that polls operations
from the replication oplog of a remote server, and
applies them to the local server. This capability
supports certain classes of real-time migrations that
require that the source server remain online and in
operation throughout the migration process.
http://docs.mongodb.org/manual/reference/program/mongooplog/
31. mongoperf
mongoperf is a utility to check disk I/O performance
independently of MongoDB.
It times tests of random disk I/O and presents the
results. You can use mongoperf for any case apart
from MongoDB.
http://docs.mongodb.org/manual/reference/program/mongoperf/
32. mongosniff
mongosniff provides a low-level operation tracing/
sniffing view into database activity in real time. Think
of mongosniff as a MongoDB-specific analogue of
tcpdump for TCP/IP network traffic. Typically,
mongosniff is most frequently used in driver
development.
http://docs.mongodb.org/manual/reference/program/mongosniff/
33. mongostat
The mongostat utility provides a quick overview of
the status of a currently running mongod or mongos
instance. mongostat is functionally similar to the
UNIX/Linux file system utility vmstat, but provides
data regarding mongod and mongos instances.
http://docs.mongodb.org/manual/reference/program/mongostat/
34. mongotop
mongotop provides a method to track the amount of
time a MongoDB instance spends reading and writing
data. mongotop provides statistics on a per-collection
level. By default, mongotop returns values
every second.
http://docs.mongodb.org/manual/reference/program/mongotop/
36. JSON
JSON (JavaScript Object Notation) is a lightweight data-interchange
http://json.org
format.
It is easy for humans to read and write.
It is easy for machines to parse and generate.
It is based on a subset of the JavaScript Programming
Language
37. JSON
JSON is built on two structures:
• A collection of name/value pairs. In various
languages, this is realized as an object, record,
struct, dictionary, hash table, keyed list, or
associative array.
• An ordered list of values. In most languages, this is
realized as an array, vector, list, or sequence.
These are universal data structures.
http://json.org
47. BSON
BSON is a binary serialization format used to store
documents and make remote procedure calls in
MongoDB. The BSON specification is located at
bsonspec.org.
http://docs.mongodb.org/manual/reference/bson-types/
48. BSON
JSON can only represent a subset of the types
supported by BSON. To preserve type information,
MongoDB adds the following extensions to the JSON
format:
http://docs.mongodb.org/manual/reference/mongodb-extended-json/
49. Strict Mode
Strict mode representations of BSON types conform
to the JSON RFC. Any JSON parser can parse these
strict mode representations as key/value pairs;
however, only the MongoDB’s internal JSON parser
also recognizes the type information conveyed by the
format.
http://docs.mongodb.org/manual/reference/mongodb-extended-json/
50. Shell mode
The MongoDB’s internal JSON parser and the mongo
shell can parse this mode.
http://docs.mongodb.org/manual/reference/mongodb-extended-json/
51. BSON-Types
Double String Object Array
Binary Data Undefined Object Id Boolean
Date Null RegExp JavaScript
http://docs.mongodb.org/manual/reference/bson-types/
Symbol
JavaScript
(w/scope)
Int32 Timestamp
Int64 MinKey MaxKey
52.
53.
54. ObjectId
ObjectId is a 12-byte BSON type, constructed using:
• a 4-byte value representing the seconds since the
Unix epoch,
• a 3-byte machine identifier,
• a 2-byte process id, and
• a 3-byte counter, starting with a random value.
http://docs.mongodb.org/manual/reference/object-id/
55. ObjectId
The relationship between the order of ObjectId values
and generation time is not strict within a single second.
If multiple systems, or multiple processes or threads on
a single system generate values, within a single second;
ObjectId values do not represent a strict insertion order.
Clock skew between clients can also result in non-strict
ordering even for values, because client drivers
generate ObjectId values, not the mongod process.
http://docs.mongodb.org/manual/reference/object-id/
62. Read
In MongoDB a query targets a specific collection of
documents. Queries specify criteria, or conditions,
that identify the documents that MongoDB returns to
the clients. A query may include a projection that
specifies the fields from the matching documents to
return. You can optionally modify queries to impose
limits, skips, and sort orders
http://docs.mongodb.org/manual/core/crud-introduction/#query
63. Read
In MongoDB, queries select documents from a single
collection.
http://docs.mongodb.org/manual/core/read-operations-introduction/
65. Write
A write operation is any operation that creates or
modifies data in the MongoDB instance. In
MongoDB, write operations target a single collection.
All write operations in MongoDB are atomic on the
level of a single document.
There are three classes of write operations in
MongoDB: insert, update, and remove.
No insert, update, or remove can affect more than
one document atomically.
http://docs.mongodb.org/manual/core/write-operations-introduction/
66. Create
Create operations add new documents to a
collection. In MongoDB, the db.collection.insert()
method perform create operations.
http://docs.mongodb.org/manual/core/write-operations-introduction/
67. Update
db.collection.update() and the db.collection.save()
methods perform update operations. The
db.collection.update() method can accept query
criteria to determine which documents to update as
well as an option to update multiple rows.
http://docs.mongodb.org/manual/core/write-operations-introduction/
68. Delete
Delete operations remove documents from a
collection. In MongoDB, db.collection.remove()
method performs delete operations. The
db.collection.remove() method accepts a query
criteria to determine which documents to remove.
http://docs.mongodb.org/manual/core/write-operations-introduction/
78. Write Isolation
The modification of a single document is always
atomic, even if the write operation modifies multiple
sub-documents within that document.
For write operations that modify multiple documents,
the operation as a whole is not atomic, and other
operations may interleave.
http://docs.mongodb.org/manual/core/write-operations-introduction/
79. Write-Concern
Write concern describes the guarantee that
MongoDB provides when reporting on the success of
a write operation. The strength of the write concerns
determine the level of guarantee.
Fast Slow
Unacknowledged Acknowledged Journaled Replica Acknowledged
Weak Strong
http://docs.mongodb.org/manual/core/write-concern/
80. Unacknowledged
With an unacknowledged write concern, MongoDB
does not acknowledge the receipt of write
operations. Unacknowledged is similar to errors
ignored; however, drivers will attempt to receive and
handle network errors when possible. The driver’s
ability to detect network errors depends on the
system’s networking configuration.
http://docs.mongodb.org/manual/core/write-concern/
82. Acknowledged
With a receipt acknowledged write concern, the
mongod confirms the receipt of the write operation.
Acknowledged write concern allows clients to catch
network, duplicate key, and other errors.
http://docs.mongodb.org/manual/core/write-concern/
84. Journaled
With a journaled write concern, the MongoDB
acknowledges the write operation only after
committing the data to the journal. This write concern
ensures that MongoDB can recover the data
following a shutdown or power interruption.
http://docs.mongodb.org/manual/core/write-concern/
86. Replica Acknowledged
Replica sets present additional considerations with
regards to write concern.. The default write concern
only requires acknowledgement from the primary.
With replica acknowledged write concern, you can
guarantee that the write operation propagates to
additional members of the replica set
http://docs.mongodb.org/manual/core/write-concern/
88. Read Isolation
MongoDB allows clients to read documents inserted
or modified before it commits these modifications to
disk, regardless of write concern level or journaling
configuration.
http://docs.mongodb.org/manual/core/write-concern/
89. Read Isolation
• For systems with multiple concurrent readers and
writers, MongoDB will allow clients to read the
results of a write operation before the write
operation returns.
• If the mongod terminates before the journal
commits, even if a write returns successfully,
queries may have read data that will not exist after
the mongod restarts.
http://docs.mongodb.org/manual/core/write-concern/
90. Durability
Data is flushed every 60 seconds.
Journal every 100ms if on the same volume of the data,
every 30ms if on a different disk
Journaled write concern set the journaling interval to 1/3
91. demo
Download and install http://robomongo.org
db.tmp.insert({ x : 1 });
db.runCommand({
"getLastError" : 1,
"j" : true
});
db.serverStatus();
93. Aggregation
Aggregations are operations that process data
records and return computed results. MongoDB
provides a rich set of aggregation operations that
examine and perform calculations on the data sets.
Running data aggregation on the mongod instance
simplifies application code and limits resource
requirements.
http://docs.mongodb.org/manual/core/aggregation-introduction/
94. Aggregation
Like queries, aggregation operations in MongoDB use
collections of documents as an input and return results
in the form of one or more documents.
http://docs.mongodb.org/manual/core/aggregation-introduction/