Storage dei dati con MongoDB

Preview:

Citation preview

Storage dei dati con

#devmean

The Forrester Wave™: Big Data NoSQL, Q3 2016

automatic scaling

Document Database high performance high availability

Collections

A grouping of MongoDB documents. A collection is the equivalent of an RDBMS table. A collection exists within a single database. Collections do not enforce a schema.

A record in MongoDB is a document, which is a data structure composed of field and value pairs. MongoDB documents are similar to JSON objects. The values of fields may include other documents, arrays, and arrays of documents.

The advantages of using documents are:• Documents (i.e. objects) correspond to native data types in many programming languages.• Embedded documents and arrays reduce need for expensive joins.• Dynamic schema supports fluent polymorphism.

{ "_id" : ObjectId("54c955492b7c8eb21818bd09"), "address" : { "street" : "2 Avenue", "zipcode" : "10075", "building" : "1480", "coord" : [ -73.9557413, 40.7720266 ] }, "borough" : "Manhattan", "cuisine" : "Italian", "grades" : [ { "date" : ISODate("2014-10-01T00:00:00Z"), "grade" : "A", "score" : 11 }, { "date" : ISODate("2014-01-16T00:00:00Z"), "grade" : "B", "score" : 17 } ], "name" : "Vella", "restaurant_id" : "41704620"}

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.

Double Boolean 64-bit integer

String Date Decimal128

Object Null Min key

Array Regular Expression Max key

Binary data JavaScript

ObjectId 32-bit integer

ObjectId

ObjectIds are small, likely unique, fast to generate, and ordered.

ObjectId values consists of 12-bytes, where the first four bytes are a timestamp that reflect the ObjectId’s creation, specifically:

• 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.

In MongoDB, each document stored in a collection requires a unique _id field that acts as a primary key. If an inserted document omits the _id field, the MongoDB driver automatically generates an ObjectId for the _id field.

ObjectId valuesDO NOT

represent a strict insertion order.

Indexes

MongoDB creates a unique index on the _id field during the creation of a collection.

Default _id Index

You cannot drop the index on the _id field.

db.records.createIndex( { score: 1 } )

Single field Indexes

sor t o rder does not matter for a sort operation

db.products.createIndex( { "item": 1, "stock": 1 } )

Compound Indexes

sort order can matter in determining whether the index can support a sort operation

MultikeyText2dsphere

2d HashedTTL

db.people.createIndex( { zipcode: 1 }, { background: true, sparse: true })

db.people.createIndex({ zipcode: 1 }, { background: true, sparse: true })

Aggregations

aggregation framework is modeled on the concept of data processing pipelines.

Map / Reduce

All map-reduce functions are JavaScript and run within the mongod process

StorageEngines

The storage engine is the component of the database that is responsible for managing how data is stored, both in memory and on disk.

MongoDB supports multiple storage engines, as different engines perform better for specific workloads

Choosing the appropriate storage engine for your use

case can significantly impact the performance of

your applications

WiredTiger is the default storage engine starting in MongoDB 3.2. It is well-suited for most workloads and is recommended for new deployments

WiredTiger provides a document-level concurrency model, checkpointing, and compression, among other features

MMAPv1 is the original MongoDB storage engine and is the default storage engine for MongoDB versions before 3.2. It performs well on workloads with high volumes of reads and writes, as well as in-place updates.

The In-Memory Storage Engine is available in MongoDB Enterprise. Rather than storing documents on-disk, it retains them in-memory for more predictable data latencies.

https://www.percona.com/blog/2015/12/23/percona-server-for-mongodb-storage-engines-in-iibench-insert-workload/

Scaling & Availability

ienumerable.it

andreabalducci

mtb.snowboard