From Coders.Bay Wiki
Jump to navigation Jump to search


MongoDB is one of the most popular and advanced non-relational (NoSQL) databases, also known as document databases. It is an open source database and offers various benefits. Unlike relational databases, MongoDB works with records called documents instead of tables and has the major advantage to structure data in a flexible way, easy to process for computers and easier to read for users. In addition to efficiency, with MongoDB a highly scalability is given.


The idea for MongoDB came in 2007 after the company DoubleClick faced issues with scalability and flexibility using existing database systems in serving 400.000 ads per second. This inspired the company 10gen, which was founded by former DoubleClick founders , to design a database, where all data is stored in JSON like documents, which are organized into collections where they can be queried. The idea was to manage data easy for the computer to process and easier for humans. In 2013 10gen was renamed to MongoDB inc. The name MongoDB was inspired by the word humongous. MongoDB was released in February 2009 as Open Source.

MongoDB feature highlights[edit]

Fault Tolerance[edit]

This is natively built into MongoDB by keeping redundant copies of the same data on different servers. A single server failure doesn’t affect the application


MongoDB seamlessly scales across multiple servers to store and process data. As data volumes and performance requirements grow, it is possible to just add more servers instead of upgrading mainframes.

Data Near Users[edit]

MongoDB lets you move data where you need it, so you can keep data near to users around the globe for fast access. The combination of MongoDB’s document model and distributed systems components is what gives MongoDB such an advantage over relational databases

Support ad hoc queries[edit]

An ad hoc query is a short-lived command whose value depends on a variable. Each time an ad hoc query is executed, the result may be different, depending on the variables in question. In MongoDB, you can search by field, range query and it also supports regular expression searches.

Scalability and duplication of data[edit]

MongoDB seamlessly scales across multiple servers to store and process data. As data volumes and performance requirements grow, it is possible to just add more servers instead of upgrading mainframes. The data is duplicated to keep the system up and also keep its running condition in case of hardware failure.

Load balancing[edit]

It has an automatic load balancing configuration because of data placed in shards.

Uses JavaScript instead of Procedures[edit]

It also supports:[edit]

- JSON data model with dynamic schemas

- Auto-sharding for horizontal scalability

- Built in replication for high availability

Main Functions & Syntax[edit]

The syntax in MongoDB is similar to JSON (JavaScript Object Notation)and is called BSON (Binary JSON). BSON’s binary structure encodes type and length information, which allows it to be parsed much more quickly. Let’s have a look at some basic CRUD functions:

Create Collection & data record[edit]

As mentioned, MongoDB is a NoSQL database, which follows non-relational approaches. Data is stored in records, which are called documents. Instead of tables, there are Collections in MongoDB. The advantage is that documents are way more flexible than data in relational databases.

E.g. in one document we can store a student (Name, last name, address,...) with multiple data like a second phone number or a second address without changing the structure. Which means that documents are not restricted to have the same number of columns.

Basic Syntax for creating a new Collection[edit]

db.createCollection(collectionName, options)

The options parameter is optional to specify the configuration of the Collection.

Basic Syntax for a data record (document)[edit]


- inserts a document or documents into a collection


                   _id : ObjectId("507f191e810c19729de860ea"),
                   Name: "Senad",
                   Last_Name: "Kurtovic",
                   Address: "Nowherestreet 14"

“persons” is the name of the Collection, insert is the basic method for recording data. The _id, if not given is generated automatically and unique in MongoDB. It is a 12 bytes hexadecimal number.

Collections can be defined separately, the data record can also be done immediately. In our case, if we start inserting while “persons” doesn’t exist yet, it will be created automatically.

Basic Syntax for querying documents[edit]

The find() method is basically used for querying data in a MongoDB Collection:


All documents will be displayed in a non-structural way.


db.persons.find(){ "_id" : ObjectId("507f191e810c19729de860ea"), 
                   "Name" : "Senad", 
                   "Last_Name" : "Kurtovic", 
                   "Address" : "Nowherestreet 14" 

-- or

                   "_id" : ObjectId("507f191e810c19729de860ea"), 

-- or

                   "Name" : "Senad"

For more functions and detailed informations visit the official MongoDB Documentation.

Installation, Guides and Tutorials[edit]

MongoDB supports a variety of 64-bit platforms. For a list and detailed instructions for the installation, please refer to the Guide section of the officialMongoDB documentation (recommended):

The following guide is a quick summary of how to install MongoDB (for Windows) locally:

  1. Download the binaries from the MongoDB Download Center
  2. Open the Windows Explorer
  3. Open the downloaded .msi file in your Download path
  4. The Windows installer guides you through the installation
  5. Run MongoDB (‘mongod.exe’ if you run it from your CLI)
  6. Now you can set up the MongoDB environment
  7. Connect to MongoDB

In addition, there are also GUI admin tools like RoboMongo (or Robo3T) provides the users a simplified workflow for handling the databases in MongoDB, when it becomes unmanageable to operate huge volumes of data from the command-line tool.

A variety of tutorials for how to get started with MongoDB are offered, some of them are:

Official MongoDB Manual , visit also:

Tutorials Point

Java T point

MongoDB Tutorial

A MongoDB Cheat Sheet on GitHub

by Senad Kurtovic