Download - Mongo db halloween party
Material from mongodb.org licensed by MongoDB under Creative Commons. MongoDB, Mongo, and the leaf logo are registered trademarks of MongoDB, Inc.
halloweenParty!
halloweenParty!
@andreabalducci
#mhp14
intro
MongoDBMongoDB (from "humongous") is an open-source document database, and the leading NoSQL database.
http://www.mongodb.org
MongoDB is a document database that provides high performance, high availability, and easy scalability.
Intro • 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/
Document Oriented StorageJSON-style documents with dynamic schemas offer
simplicity and power.
Server
Document Oriented Storage
Database
Collection
Document
{key : value}
Full Index Support
Index on any attribute, just like you're used to.
Full Index SupportType Description
Single FieldA 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.
ReplicationReplica sets provide redundancy and high availability,
and are the basis for all production deployments.
Auto-Sharding
Scale horizontally without compromising functionality
Querying
Rich, document-based queries.
Fast In-Place Updates
Atomic modifiers for contention-free performance.
Map / Reduce
Flexible aggregation and data processing.
GridFS
Store files of any size without complicating your stack
MMS
Monitoring and backup designed for MongoDB.
Programs
Programsbsondump mongooplog
mongo mongoperf
mongod mongorestore
mongodump mongos
mongoexport mongosniff
mongofiles mongostat
mongoimport mongotop
mongo
http://docs.mongodb.org/manual/reference/program/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.
mongo --nodb
MongoDB shell version: 2.6.1 > "m"+4+'n'+(5*2+2+2)+'abc Man'.substr(2)+'sion'
> [1,2,3,4].forEach(function (n) { print(n); })
> exit
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/
mongosmongos 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/
mongodumpmongodump 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/
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/
mongorestoreThe 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/
mongoexportmongoexport 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/
mongoimportThe 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/
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/
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/
mongoperfmongoperf 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/
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/
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/
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/
Json
JSON
http://json.org
JSON (JavaScript Object Notation) is a lightweight data-
interchange 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
JSONJSON 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
{ "username": { "first": "Andrea", "last": "Balducci" }, "age": 40, "tags": ["mongodb", "elasticsearch", "eventsourcing", "ddd"], "groups": [{ "_id": 1, "name": "DevMarche" }, { "_id": 2, "name": "XPUG Marche" }] }
json
http://jsbeautifier.org
BSON
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/
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/
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/
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/
BSON-Types
http://docs.mongodb.org/manual/reference/bson-types/
Double String Object Array
Binary Data Undefined Object Id Boolean
Date Null RegExp JavaScript
Symbol JavaScript(w/scope) Int32 Timestamp
Int64 MinKey MaxKey
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/
ObjectIdThe 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/
crud
bat = {/*oo*/}
db.halloween.insert(bat)
db.get
db.getCollectionNames()
db.halloween.f
[TAB]
[TAB][ENT]
[ENT]
[ENT]
[ENT]
db.halloween.find() [ENT]
[ENT]
[ENT]
bat
db.halloween.insert
… if ( typeof(obj._id) == "undefined" && !Array.isArray(obj) ){ var tmp = obj; // don't want to modify input obj = {_id: new ObjectId()}; for (var key in tmp){ obj[key] = tmp[key];
} } …
db.halloween.insert
It’s the client baby
ReadIn 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
ReadIn MongoDB, queries select documents from a single collection.
http://docs.mongodb.org/manual/core/read-operations-introduction/
MongoDB - SQL
http://docs.mongodb.org/manual/reference/sql-comparison/
WriteA 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/
CreateCreate 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/
Updatedb.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/
DeleteDelete 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/
tags = []; [1,2,3,4].forEach(function(num){ tags.push(''+num); db.halloween.insert({ num: NumberInt(num), tags:tags }) })
Sample Data
db.halloween.find({
})
Find All
db.halloween.find({ tags:'2' })
Array filter
db.halloween.find({ tags:'2', num: 2 })
And
db.halloween.find({ tags:'1', tags:'3' })
tags = ‘1’ AND tags=‘3’
1,2,3…..3,2,1
db.halloween.find({ tags:'1', tags:'3' })
tags = ‘1’ AND tags=‘3’
db.halloween.find({ tags:'3', tags:'1' })
tags = ‘1’ AND tags=‘3’
JSON!
db.halloween.find({ tags:'3', tags:'1' })
/kəˈpʊt/
Write IsolationThe 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/
Write-ConcernWrite 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.
http://docs.mongodb.org/manual/core/write-concern/
Weak Strong
Unacknowledged Acknowledged Journaled Replica Acknowledged
Fast Slow
UnacknowledgedWith 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/
Unacknowledged
http://docs.mongodb.org/manual/core/write-concern/
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/
Acknowledged
http://docs.mongodb.org/manual/core/write-concern/
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/
Journaled
http://docs.mongodb.org/manual/core/write-concern/
Replica AcknowledgedReplica 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/
Replica Acknowledged
http://docs.mongodb.org/manual/core/write-concern/
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/
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/
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
demoDownload and install http://robomongo.org
db.tmp.insert({ x : 1 });
db.runCommand({ "getLastError" : 1, "j" : true });
db.serverStatus();
Aggregationframework
Aggregation
http://docs.mongodb.org/manual/core/aggregation-introduction/
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.
Aggregation
http://docs.mongodb.org/manual/core/aggregation-introduction/
Like queries, aggregation operations in MongoDB use
collections of documents as an input and return results
in the form of one or more documents.
Data Processing Pipeline
http://docs.mongodb.org/manual/core/aggregation-introduction/
Operator Reference
http://docs.mongodb.org/manual/reference/operator/
aggregation/#aggregation-pipeline-operator-reference
Trick or treat
?
http://tinyurl.com/mohapa
thanks!
@andreabalducci
#mhp14