Why are developers interested in MongoDB?
MongoDB is a database management system designed to rapidly develop web applications and internet infrastructure.
MongoDB's document format is based on JSON,a popular schema for storing arbitrary data structures.JSON is an acronym for JavaScript Object Notation.
1.1 Built for the internet
1.2 MongoDB's key features
- Document data model
- MongoDB's data model is document-oriented.
- A JSON document needs double quotes everywhere except for numeric values.
- MongoDB stores documents in a format called Binary JSON,or BSON.
2.Schema Model Advantages
This lack of imposed schema confers some advantages.
- First,your application code, and not the database,enforces the data's structure. This can speed up initial application development when the schema is changing frequently.
- Second,and more significantly,a schema-less model allows you to represent data with truly variable properties.
3.Ad hoc queries
to say that a system supports ad hoc queries is to say that isn't necessary to define in advance what sorts of queries the system will accept.
4.Indexes
Indexes in MongoDB are implemented as a B-tree data structure.WiredTiger has support for long-structured merge-trees(LSM) that's expected to be available in the MongoDB 3.2 production release.
Permitting multiple secondary indexes MongoDB allows users to optimize for a wide variety of queries.
With MongoDB,you can create up to 64 indexes per collection.The kinds of indexes supported include all the ones you'd find in an RDMBS;ascending,descending,unique,compound-key,hashed,text,and even geospatial indexes are supported.
5.Replication
MongoDB provides database replication via to topology known as a replica set.
- Replica sets distribute data across two or more machines for redundancy and automate failover in the event of server and network outages.
- replication is used to scale database reads.
6.Speed and durability
- Write speed can be understood as the volume of inserts,updates, and deletes that a database can process in a given time frame.
- Durability refers to level of assurance that these write operations have been made permanent.
In MongoDB's case ,users control the speed and durability trade-off by choosing write semantics and deciding whether to enable journaling.Journaling is enabled by default since MongoDB v2.0.
7.Scaling
The easiest way to scale most databases is to upgrade the hardware.
1.3 MongoDB's core server and tools
1.3.1 Core server
The core database server runs via an executable called mongod(mongodb.exe on Windows).The mongod server process receives commands over a network socket using a custom binary protocol.
1.3.2 JavaScript shell
1.3.3 Database drivers
The driver is the code used in an application to communicate with a MongoDB server.All drivers have functionality to query,retrive results, write data ,and run database commands.
1.3.4 Commnad-line tools
MongoDB is bundled with several command-line utilities :
- mongodump and mongorestore ----- Standard utilities for backing up and restoring a database.mongodump saves the databases's data in its native BSON format and thus is best used for backups only;this tool has the advantage of being usable for hot backups,which can easily be restored with mongorestore.
- mongoexport and mongoimport ---Export and import JSON,CSV, and TSV^7 data. mongoimport can also be good for initial imports of large data sets
- mongosniff --- A wire-sniffing tool for viewing operations sent to the database.
- mongostat --- Similar to iostat, this utility constantly polls MongoDB and the system to provide helpful stats,including the number of operations per second(inserts,queries,updates,deletes, and so on), the amount of virtual memory allocated, and the number of connections to the server.
- mongotop --- Similar to top, this utility polls MongoDB and shows the amount of time it spends reading and writing data in each collection.
- mongoperf --- Helps you understand the disk operations happening in a running MongoDB instance.
- mongooplog --- Shows what's happening in the MongoDB oplog.
- Bsondump --- Converts BSON files into human-readable formats including JSON.
1.4 Why MongoDB?
1.4.1 MongoDB versus other databases
Examples | Data model | Scalability model | Use cases | |
Simple key-value stores | Memcached | key-value,where the value is a binary blob. | Variable.Memcached can scale across nodes, converting all available RAM into a single,monolithic datastore. | Caching.Web ops. |
Sophisticated value stores | HBase,Cassandra,Riak KV,Redis ,CouchDB | Variable.Cassandra uses a key-value structure known as a column.HBase and Redis store binary blobs.CouchDB stores JSON documents. | Eventually consistent,multinode distribution for high availability and easy failover | High-throughput verticals(activityfeeds,message queues).Caching. Web ops. |
Relational Databases | Oracle Database, IBM DB2,Microsoft SQL Server,MySQL, PostgreSQL | Tables | Vertical scaling.Limited support for clustering and manual partitioning. | System requiring transactions (banking,finance) or SQL.Normalized data model. |
1.5 Summary
To summarize,MongoDB is an open source. document-based database management system.Designed for the data and scalability requirements of modern internet applications.MongDB features dynamic queries and secondary indexes,fast atomic updates and complex aggregations, and support for replication with automatic failover and sharding for scaling horizontally.