MongoDB is an open-source document database that provides high performance, high availability, and automatic scaling.
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:
MongoDB provides high performance data persistence. In particular,
MongoDB supports a rich query language to support read and write operations (CRUD) as well as:
MongoDB’s replication facility, called replica set, provides:
A replica set is a group of MongoDB servers that maintain the same data set, providing redundancy and increasing data availability.
MongoDB provides horizontal scalability as part of its core functionality:
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.
MongoDB stores BSON documents, i.e. data records, in collections; the collections in databases.
In MongoDB, databases hold collections of documents.
To select a database to use, in the mongo
shell, issue the use <db>
statement, as in the following example:
use myDB
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 database myNewDB
and the collection myNewCollection1
if they do not already exist.
For a list of restrictions on database names, see Naming Restrictions.
MongoDB stores documents in collections. Collections are analogous to tables in relational databases.
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 the createIndex()
operations create their respective collection if they do not already exist.
For a list of restrictions on collection names, see Naming Restrictions.
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
.
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.
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 fields first
and last
.birth
and death
hold values of the Date type.contribs
holds an array of strings.views
holds a value of the NumberLong type.
Field names are strings.
Documents have the following restrictions on field names:
_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.$
) character..
) character.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.
For indexed collections, the values for the indexed fields have a Maximum Index Key Length
limit. SeeMaximum Index Key Length
for details.
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: