2014年12月28日星期日

MongoDB Notes Part I

READ

For query operations, MongoDB provides a db.collection.find( ) method. The method accepts both the query criteria and projections and returns a cursor to the matching documents.
  • All queries in MongoDB address a single document.
  • In aggregation pipeline, the $match pipeline stage provides access to MongoDB queries.

Projections are the second argument of find( )
  • include: 1
  • exclude: 0
    • db.records.find( { "user_id": { $lt: 42 } }, { "history": 0 } )
  • For fields that contain arrays, MongoDB provides the following projection operateors:
    • $elemMatch
    • $slice
    • $

Create an index on a field
  • db.inventory.ensureIndex( { type: 1 } )
Covering a Query
  • all the fields in the query are part of an index
  • all the fields returned in the results are in the same index

Sharded clusters allow you to partition a data set among a cluster of mongod instances in a way that is nearly transparent to the application.

When the query contains a shard key, could use cluster metadata from config database to route the query to the specify shard:

If a query does not contain a shard key, direct the query to all shards in the cluster and this would be quite inefficient:

WRITE
There are three kinds of classes of operations:
  • insert
  • update
  • remove





The modification of a single document is always atomic. If a write operation modifies multiple documents, the operation as a whole is not atomic.

Write Concern Level:
  • Unacknowledged
    • MongoDB does not acknowledge the receipt of write operations, something similar to errors ignored.
  • Acknowledged
    • Mongod confirms that it received the write operations and applied the change to the in-memory view of data.
  • Journaled
    • MongoDB acknowledges the write operation only after committing the data to the journal. This ensures that MongoDB can recover the data following a shutdown or power interruption.
  • Replica Acknowledged
    • Guarantee that write operations propagates to additional members of the replica set.

$isolated operator does not provide “all or nothing” atomicity. It does not work on sharded clusters.

Distributed Write Operations:
The mongos directs write operations from applications to the shards that are responsible for the specific portion of data set. It use cluster metadata from config database to route the write operation to appropriate shard.

MongoDB partitions data in a sharded collection into ranges based on the values of the shard key.

Write Operation on Replica Sets
  • Frist go to the primary, records the operations on primary’s oplog
  • Secondary members replicating the oplog and applying the operations to themselves asynchronously

MongoDB uses write ahead logging to an on-disk journal to guarantee write operation durability and to provide crash resiliency. 

MongoDB stores data in the form of BSON documents, which are rich mappings of keys, or field names, to values. 

Every document in MongoDB is stored in a record. All records are part of a collection, which is a logical grouping of documents in a MongoDB database. The documents in a collection share a set of indexes, and have the same fields and structures. A database construct is a group of related collections. 

Record Allocation Strategies


  • Power of 2 Sized Allocations
  • Exact Fit Allocations
All pictures are from http://docs.mongodb.org/manual/

没有评论:

发表评论