You are on page 1of 3

Introduction to MongoDB

MongoDB is a general purpose, powerful, flexible and scalable open-source NO-SQL


database emerged by MongoDB, Inc. It is written in C++. It integrates the potential to scale
out with features such as secondary indexes, range queries, sorting, aggregations and
geospatial indexes. It stores data in JSON like documents that can differ in structure.
Basically it is a document-oriented database, not a relational one that provides high
availability, high performance and automatic scaling. It operates on the notion of collection
and document. It works with dynamic schemas, implies that you can create records without
first defining the structures such as the fields or the types of their values.

The following table outlines some of the common notions in MySQL and MongoDB.

MySQL MongoDB
Database Database
Table Collection
Row Document
Column Field
Joins Embedded documents
& Linking
Foreign Key Reference

Like MySQL, MongoDB also provides rich set of features and functionality far beyond those
provided in simple key-value stores.

Features of MongoDB

In this section, we’ll see the features of MongoDB that allows us to operate efficiently.

 Document-Oriented Storage
 Full Index Support
 Replication and high availability
 Auto-Sharding
 Fast In-Place Updates
 Map/Reduce
 GridFS
 Professional Support by MongoDB
Documents

The smallest unit which plays a vital role in MongoDB is the document. It is simply
analogous to a row in a relational database but there is one big difference ie. not every
document requires to have the interchangeable schema – each of them can have distinct fields
and that is a very functional feature in current situation. MongoDB doesn't enforce a schema
like RDBMS. In short, a document is a multi dimensional array. In the context of MongoDB,
document is a JSON array. MongoDB documents are composed of field and value pairs and
have the following structure:
{

Field1: Value1,

Field2: Value2,

.....

FieldN:ValueN

The value of a field can be any of the BSON data types which will be discussed later in the
chapter.

For example,

Name: Vimala, Qualification: MSc

Collections

A Collection is a collection of documents and indexes. It is equivalent to a table of an


RDBMS. It is most commonly used to store the documents those who are not identical in
structure. In RDBMS, a schema defines the structure of data in a database. MongoDB does
not need such a set of formula defining the structure of data. So, it is quite possible to store
documents of changing structures in a collection. Practically, you don’t need to define a
column and its data type unlike in RDBMS, while working with MongoDB.

In the following example, it is shown that two MongoDB documents, which belongs to same
collection, storing data of different structures.

{Name:Vimala} {Age:28}

Rules for naming collections:

1. It must begin with letters or an underscore.

2. It may contain digits.

3. Don’t use “$” character within the name of a collection. $ is reserved character in
MongoDB.

4. It must not exceed 128 characters.

You can create collection in MongoDB either explicitly or implicitly.

db.createCollection(name, options) - Creates a new collection explicitly.


The db.createCollection() method has the following parameters:

Parameter Type Description

name string Name of the collection to be created.

options document Optional. Configuration options for memory size and indexing

db.collection_name.insert({Field1:Value1, Field2:Value2}) - Creates a new collection


implicitly.

The above syntax inserts the documents into the collection. If the collection does not exist,
the insert() method creates the collection.

For example,

db.student.insert({Name:Vimala})

The insert() method creates the collection ‘student’ automatically with one document. You
can check the created collection by using the command ‘show collections’

>show collections

student

system.indexes

You might also like