MongoDB is an open-source document database that provides high performance, high availability, and automatic scaling.
Document Database
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.
Key Features
High Performance
MongoDB provides high performance data persistence. In particular,
- Support for embedded data models reduces I/O activity on database system.
- Indexes support faster queries and can include keys from embedded documents and arrays.
Rich Query Language
MongoDB supports a rich query language to support read and write operations (CRUD) as well as:
High Availability
MongoDB’s replication facility, called replica set, provides:
- automatic failover and
- data redundancy.
A replica set is a group of MongoDB servers that maintain the same data set, providing redundancy and increasing data availability.
Horizontal Scalability
MongoDB provides horizontal scalability as part of its core functionality:
Support for Multiple Storage Engines
MongoDB supports multiple storage engines, such as:
In addition, MongoDB provides pluggable storage engine API that allows third parties to develop storage engines for MongoDB.
Databases and Collections
MongoDB stores BSON documents, i.e. data records, in collections; the collections in databases.
Databases
In MongoDB, databases hold collections of documents.
To select a database to use, in the
mongo
shell, issue theuse <db>
statement, as in the following example:use myDB
Create a Database
If a database does not exist, MongoDB creates the database when you first store data for that database. As such, you can switch to a non-existent database and perform the following operation in the
mongo
shell:use myNewDB db.myNewCollection1.insertOne( { x: 1 } )
The
insertOne()
operation creates both the databasemyNewDB
and the collectionmyNewCollection1
if they do not already exist.For a list of restrictions on database names, see Naming Restrictions.
Collections
MongoDB stores documents in collections. Collections are analogous to tables in relational databases.
Create a Collection
If a collection does not exist, MongoDB creates the collection when you first store data for that collection.
db.myNewCollection2.insertOne( { x: 1 } ) db.myNewCollection3.createIndex( { y: 1 } )
Both the
insertOne()
and thecreateIndex()
operations create their respective collection if they do not already exist.For a list of restrictions on collection names, see Naming Restrictions.
Explicit Creation
MongoDB provides the
db.createCollection()
method to explicitly create a collection with various options, such as setting the maximum size or the documentation validation rules. If you are not specifying these options, you do not need to explicitly create the collection since MongoDB creates new collections when you first store data for the collections.To modify these collection options, see
collMod
.Documents
MongoDB stores data records as BSON documents. BSON is a binary representation of JSONdocuments, though it contains more data types than JSON. For the BSON spec, see bsonspec.org. See also BSON Types.
Document Structure
MongoDB documents are composed of field-and-value pairs and have the following structure:
{ field1: value1, field2: value2, field3: value3, ... fieldN: valueN }
The value of a field can be any of the BSON data types, including other documents, arrays, and arrays of documents. For example, the following document contains values of varying types:
var mydoc = {
_id: ObjectId("5099803df3f4948bd2f98391"), name: { first: "Alan", last: "Turing" }, birth: new Date('Jun 23, 1912'), death: new Date('Jun 07, 1954'), contribs: [ "Turing machine", "Turing test", "Turingery" ], views : NumberLong(1250000) }
The above fields have the following data types:
_id
holds an ObjectId.name
holds an embedded document that contains the fieldsfirst
andlast
.birth
anddeath
hold values of the Date type.contribs
holds an array of strings.views
holds a value of the NumberLong type.Field Names
Field names are strings.
Documents have the following restrictions on field names:
- The field name
_id
is reserved for use as a primary key; its value must be unique in the collection, is immutable, and may be of any type other than an array. - The field names cannot start with the dollar sign (
$
) character. - The field names cannot contain the dot (
.
) character. - The field names cannot contain the
null
character.
BSON documents may have more than one field with the same name. Most MongoDB interfaces, however, represent MongoDB with a structure (e.g. a hash table) that does not support duplicate field names. If you need to manipulate documents that have more than one field with the same name, see the driver documentation for your driver.
Some documents created by internal MongoDB processes may have duplicate fields, but no MongoDB process will ever add duplicate fields to an existing user document.
Field Value Limit
For indexed collections, the values for the indexed fields have a
Maximum Index Key Length
limit. SeeMaximum Index Key Length
for details.Arrays
To specify or access an element of an array by the zero-based index position, concatenate the array name with the dot (
.
) and zero-based index position, and enclose in quotes:"<array>.<index>"
For example, given the following field in a document:
{ ... contribs: [ "Turing machine", "Turing test", "Turingery" ], ... }
To specify the third element in the
contribs
array, use the dot notation"contribs.2"
.For examples querying arrays, see:
- The field name
