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.

A MongoDB document.

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.

    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:

    • Sharding distributes data across a cluster of machines.
    • Starting in 3.4, MongoDB supports creating zones of data based on the shard key. In a balanced cluster, MongoDB directs reads and writes covered by a zone only to those shards inside the zone. See the Zones manual page for more information.

    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.

    A collection of MongoDB documents.

    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

    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 database myNewDB and the collection myNewCollection1 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 the createIndex() 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.

    A MongoDB document.

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

      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:

Source: Introduction to MongoDB — MongoDB Manual 3.6

ThirdEye Data

Transforming Enterprises with
Data & AI Services & Solutions.

ThirdEye delivers Data and AI services & solutions for enterprises worldwide by
leveraging state-of-the-art Data & AI technologies.

Talk to ThirdEye
CONTACT US