Mongo db halloween party

101
Material from mongodb.org licensed by MongoDB under Creative Commons. MongoDB, Mongo, and the leaf logo are registered trademarks of MongoDB, Inc.

description

MongoDb Halloween Party. DevMarche @ cowoancona

Transcript of Mongo db halloween party

Page 1: 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.

Page 2: Mongo db halloween party

halloweenParty!

Page 3: Mongo db halloween party

halloweenParty!

@andreabalducci

#mhp14

Page 4: Mongo db halloween party

intro

Page 5: Mongo db halloween party

MongoDBMongoDB (from "humongous") is an open-source document database, and the leading NoSQL database.

http://www.mongodb.org

Page 6: Mongo db halloween party

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/

Page 7: Mongo db halloween party

Document Oriented StorageJSON-style documents with dynamic schemas offer

simplicity and power.

Page 8: Mongo db halloween party

Server

Document Oriented Storage

Database

Collection

Document

{key : value}

Page 9: Mongo db halloween party

Full Index Support

Index on any attribute, just like you're used to.

Page 10: Mongo db halloween party

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.

Page 11: Mongo db halloween party

ReplicationReplica sets provide redundancy and high availability,

and are the basis for all production deployments.

Page 12: Mongo db halloween party

Auto-Sharding

Scale horizontally without compromising functionality

Page 13: Mongo db halloween party

Querying

Rich, document-based queries.

Page 14: Mongo db halloween party

Fast In-Place Updates

Atomic modifiers for contention-free performance.

Page 15: Mongo db halloween party

Map / Reduce

Flexible aggregation and data processing.

Page 16: Mongo db halloween party

GridFS

Store files of any size without complicating your stack

Page 17: Mongo db halloween party

MMS

Monitoring and backup designed for MongoDB.

Page 18: Mongo db halloween party

Programs

Page 19: Mongo db halloween party

Programsbsondump mongooplog

mongo mongoperf

mongod mongorestore

mongodump mongos

mongoexport mongosniff

mongofiles mongostat

mongoimport mongotop

Page 20: Mongo db halloween party

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.

Page 21: Mongo db halloween party

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

Page 22: Mongo db halloween party

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/

Page 23: Mongo db halloween party

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/

Page 24: Mongo db halloween party

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/

Page 25: Mongo db halloween party

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/

Page 26: Mongo db halloween party

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/

Page 27: Mongo db halloween party

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/

Page 28: Mongo db halloween party

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/

Page 29: Mongo db halloween party

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/

Page 30: Mongo db halloween party

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/

Page 31: Mongo db halloween party

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/

Page 32: Mongo db halloween party

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/

Page 33: Mongo db halloween party

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/

Page 34: Mongo db halloween party

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/

Page 35: Mongo db halloween party

Json

Page 36: Mongo db halloween party

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

Page 37: Mongo db halloween party

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

Page 38: Mongo db halloween party

Object

{ "_id": "mongodb" }

http://json.org

Page 39: Mongo db halloween party

Array

["2.4.5", "2.6.0", "2.6.1"]

http://json.org

Page 40: Mongo db halloween party

Value

http://json.org

Page 41: Mongo db halloween party

String

http://json.org

Page 42: Mongo db halloween party

Number

http://json.org

Page 43: Mongo db halloween party

{ "username": { "first": "Andrea", "last": "Balducci" }, "age": 40, "tags": ["mongodb", "elasticsearch", "eventsourcing", "ddd"], "groups": [{ "_id": 1, "name": "DevMarche" }, { "_id": 2, "name": "XPUG Marche" }] }

json

Page 44: Mongo db halloween party
Page 45: Mongo db halloween party

http://jsbeautifier.org

Page 46: Mongo db halloween party

BSON

Page 47: Mongo db halloween party

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/

Page 48: Mongo db halloween party

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/

Page 49: Mongo db halloween party

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/

Page 50: Mongo db halloween party

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/

Page 51: Mongo db halloween party

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

Page 52: Mongo db halloween party
Page 53: Mongo db halloween party
Page 54: Mongo db halloween party

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/

Page 55: Mongo db halloween party

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/

Page 56: Mongo db halloween party

crud

Page 57: Mongo db halloween party

bat = {/*oo*/}

db.halloween.insert(bat)

db.get

db.getCollectionNames()

db.halloween.f

[TAB]

[TAB][ENT]

[ENT]

[ENT]

[ENT]

Page 58: Mongo db halloween party
Page 59: Mongo db halloween party

db.halloween.find() [ENT]

[ENT]

[ENT]

bat

db.halloween.insert

Page 60: Mongo db halloween party

… 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

Page 61: Mongo db halloween party

It’s the client baby

Page 62: Mongo db halloween party

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

Page 63: Mongo db halloween party

ReadIn MongoDB, queries select documents from a single collection.

http://docs.mongodb.org/manual/core/read-operations-introduction/

Page 64: Mongo db halloween party

MongoDB - SQL

http://docs.mongodb.org/manual/reference/sql-comparison/

Page 65: Mongo db halloween party

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/

Page 66: Mongo db halloween party

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/

Page 67: Mongo db halloween party

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/

Page 68: Mongo db halloween party

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/

Page 69: Mongo db halloween party

tags = []; [1,2,3,4].forEach(function(num){ tags.push(''+num); db.halloween.insert({ num: NumberInt(num), tags:tags }) })

Sample Data

Page 70: Mongo db halloween party

db.halloween.find({

})

Find All

Page 71: Mongo db halloween party

db.halloween.find({ tags:'2' })

Array filter

Page 72: Mongo db halloween party

db.halloween.find({ tags:'2', num: 2 })

And

Page 73: Mongo db halloween party

db.halloween.find({ tags:'1', tags:'3' })

tags = ‘1’ AND tags=‘3’

Page 74: Mongo db halloween party

1,2,3…..3,2,1

Page 75: Mongo db halloween party

db.halloween.find({ tags:'1', tags:'3' })

tags = ‘1’ AND tags=‘3’

Page 76: Mongo db halloween party

db.halloween.find({ tags:'3', tags:'1' })

tags = ‘1’ AND tags=‘3’

Page 77: Mongo db halloween party

JSON!

db.halloween.find({ tags:'3', tags:'1' })

/kəˈpʊt/

Page 78: Mongo db halloween party

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/

Page 79: Mongo db halloween party

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

Page 80: Mongo db halloween party

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/

Page 81: Mongo db halloween party

Unacknowledged

http://docs.mongodb.org/manual/core/write-concern/

Page 82: Mongo db halloween party

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/

Page 83: Mongo db halloween party

Acknowledged

http://docs.mongodb.org/manual/core/write-concern/

Page 84: Mongo db halloween party

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/

Page 85: Mongo db halloween party

Journaled

http://docs.mongodb.org/manual/core/write-concern/

Page 86: Mongo db halloween party

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/

Page 87: Mongo db halloween party

Replica Acknowledged

http://docs.mongodb.org/manual/core/write-concern/

Page 88: Mongo db halloween party

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/

Page 89: Mongo db halloween party

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/

Page 90: Mongo db halloween party

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

Page 91: Mongo db halloween party

demoDownload and install http://robomongo.org

db.tmp.insert({ x : 1 });

db.runCommand({ "getLastError" : 1, "j" : true });

db.serverStatus();

Page 92: Mongo db halloween party

Aggregationframework

Page 93: Mongo db halloween party

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.

Page 94: Mongo db halloween party

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.

Page 95: Mongo db halloween party

Data Processing Pipeline

http://docs.mongodb.org/manual/core/aggregation-introduction/

Page 96: Mongo db halloween party

Operator Reference

http://docs.mongodb.org/manual/reference/operator/

aggregation/#aggregation-pipeline-operator-reference

Page 97: Mongo db halloween party

Trick or treat

?

Page 98: Mongo db halloween party

http://tinyurl.com/mohapa

Page 99: Mongo db halloween party
Page 100: Mongo db halloween party
Page 101: Mongo db halloween party

thanks!

@andreabalducci

#mhp14