Document Store Database - MongoDB (Part 3)
Tags: mongodb, nosql, database
Published: 2020-07-17 06:11:41

Continuing from [part 2](https://dev.to/nyashanziramasanga/types-of-nosql-data-storages-part-2-mi8?preview=58c23eb87d232ac2b9fd7edb238575e735188199bb5177ef57f941dd73ba89b378fd8b38c818c60edbce71bfb145ece50b30b8312ad19b75de8618ad)

Document store database technology will be the primary focus after understanding how NoSQL databases work. A definition of document stores will explore the mechanisms that make it different from other NoSQL storage techniques. This will be followed by a list of characteristics and concluded by studying the most popular document database software in use MongoDB.

## What is a Document Store?

Document Store, also known as a document-oriented database store, is used to retrieve and store data in NoSQL databases as documents similar to JSON (JavaScript Object Notation) objects. They have similarities with key-value stores when storing data where a document contains pairs of fields and values. Below is an example of a MongoDB stored document which is very similar to a JSON object, this semi-structured format makes it easy for developers to work with.

Example of a coding boot camp document on a MongoDB database server:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/lp1rgz641fpbegedln0d.png)

Key-Value pairs in a MongoDB document:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/mt5i8dyw5x0oeq2uj0t9.png)

Collection of 3 documents:

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/tjld9r4sgim8zqxhrefd.png)

## Characteristics of Document Stores

Below are the main characteristics found on NoSQL document store databases:

- **JSON-like** - Stores all information of an object in a single instance similar to a JSON Object in the database
- **CRUD Operations** - Supports Create, Read, Update and Delete operations
- **Lookup** - Extracts documents based on a unique key or identifier
- **Data Retrieval** - Can use an API or query language to retrieve documents based on the content
- **Organisation** – Documents can be organised in a variety of ways for instance as Collections which are a group of documents, Tags which are additional data outside document and Directory hierarchies which are groups of documents organised in a tree-like structure
- **Schema-less** - Does not have to adhere to a predefined schema
- **Key-value** - Uses key-value pairs in documents

## MongoDB

MongoDB is a cross-platform, open-source, schema-free NoSQL database developed using C++ in 2007 by 10gen and is now known as MongoDB Inc. Prominent users of MongoDB include start-up rides hailing services Uber and Lyft.

### Database Structure

MongoDB databases reside on a MongoDB Server which can have multiple database instances. A database has collections which contain one or more documents. Figure 4 below illustrates a MongoDB database structure; In the figure below, the customer database has a collection of usernames and courses documents. The “_id” field is a primary key, a unique identifier type named ObjectId which is created by the application when the document is created.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9dskvwfnjretlet53xef.png)

**Database** – Instance of a MongoDB database on a server
**Collections** – Group of documents
**Documents** – Records as JSON objects known as BSON (Binary JSON) a serialized MongoDB format

### Features

- Indexing - Fields in documents are indexed by primary or secondary indices.
- Replication - High availability through replication sets with read and writes done on the primary replica when a primary replica fails a secondary replica is elected to be primary.
- Aggregation - An operation that computes results, MongoDB offers 3 types of aggregation pipeline, the map-reduce function and single-purpose aggregation method. The example below uses the count operation as the aggregate to find all books where the author is Tom Ford

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/6mj06l9ny161bamftojb.png)

- Transaction - Supports ACID transactions.
- Server-Side Execution - JavaScript can be used for querying, aggregation and sent directly to the database for execution.
- Sharding (Load Balancing) - MongoDB uses sharding to scale horizontally, sharding is the process of breaking up large tables into smaller chunks called shards that are spread across multiple servers (S.Choudhury, 2019).
- BSON Format - BSON is a JSON like format which is binary encoded serialization used for storing documents in collections.
- Ad hoc queries - MongoDB supports regular expression searches, field and range queries
Example: Fetching all records from a customer table with employee name NASH.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/64q2kk9j4vy15vz4xkje.png)

- Capped Collections - Fixed-size collections which will maintain insertion order
- GridFS (File Storage) - Using MongoDB as a file storage system for storing files such as images and documents. File storage is managed by GridFS which is MongoDB’s way of storing files in a database, files are stored in binary chunks which are around 256k each.

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/lqa5hyaxrr7u9joalhak.png)

- MongoDB Management Service (MMS) - Web tool for managing and tracking databases and back-ups, this also includes performance and tracking hardware metrics.

### Datatypes

The following data types in the table below are provided by MongoDB.

| Data Type | Description |
| ------------- |:-------------:| 
| Date | Date and time |
| Array | List of objects E.g. [1,2,3] | 
| Null |Missing fields|
| Object | BSON object | 
| ObjectId | 12 bytes long binary values as unique identifiers names “_id” |String, Regular Expression|
Character sequence types |Scalar types |Boolean, integer, double|

### Database Document Operations

MongoDB uses its own API to query data from a database, In the table below only the main CRUD operations will be examined namely Selection, Insert, Update and Delete.

| Queries | Description | Query |
| ------------- |:-------------:|:-------------:| 
| Selection | Used to search for a document in a collection, the find operation is equivalent to WHERE clause in SQL. | Search for a book with the title Hello World `db.books.find({title:"Hello World"});` |
|Insert| Inserting a document into a collection using the insert operation, the save operation can also be used. MongoDB automatically appends a new primary key field “_id” to the new document|E.g. Save a book called DSAs `db.books.insert({title: "DSAs"});`|
|Updates|Updating a document in the database, the first argument specifies the selection criteria of the document to be selected and the second argument is the field to change. Uses update operation, however, there are more operations used for updating records such as updateOne, updateMany and findOneAndUpdate | E.g. Update book with id 1 to COMP3771 `db.books.update({"_id":1},{"title":"DSAs"});`|
|Delete|The remove operation is used to delete a document based on the selection criteria, also findOneAndDelete operation can be used to delete a document|E.g. Delete a book with id 1 `db.books.remove({"_id:1"})`|