Free Shipping

Secure Payment

easy returns

24/7 support

An Introduction to MongoDB

 July 14  | 0 Comments

MongoDB has rapidly grown to become a popular database for web applications. It is a perfect fit for Node.JS applications, letting you write JavaScript for the client, backend, and database layer.

In our previous post, we had seen the simple steps to setup MongoDB in Windows platform. Now, let’s delve a little bit deeper into the concepts of MongoDB.

Get Skilled in Full Stack Web Development

MongoDB Architecture

MongoDB is designed to work on a cluster which distributes data across multiple servers of your architecture. You also can use MongoDB as a standalone server, without clustering. Furthermore, you can replicate your data through multiple servers. The concept of replication will be explained in the upcoming posts to provide more clarity on this topic. MongoDB provides numerous features; however, it is important to deploy an adapted architecture which best suits your business needs.
Architecture

Key Features of MongoDB

MongoDB provides some interesting features for your application and architecture that makes it popular. The goal was to create a new breed between traditional database features and the high performance of NoSQL stores. As a result, most of its key features were created to evolve beyond the limitations of other NoSQL solutions while integrating some of the abilities of relational databases. Some of the key features are:

  • MongoDb supports rich query to fetch data from the database.
  • MongoDb supports replacing an entire document (database) or some specific fields with it’s update() command.
  • MongoDb supports Map/Reduce framework for batch processing of data and aggregation operation.
  • GridFS specification of MongoDb supports storage of very large files.
  • MongoDb supports various programming languages like C, C# and .NET, C++, Erlang, Haskell, Java, Javascript, Perl, PHP, Python, Ruby, Scala (via Casbah).
  • It supports Server-side JavaScript execution. Which allows a developer to use a single programming language for both client and server side code.
  • MongoDb is easily installable.

Next we will be going to discuss about Data formats used and connections in MongDB.

MongoDB Data format

Let’s now discuss a brief example of some of the data formats used:

{
    "_id" : ObjectId("56951380c9b9fdsdda4dc4ab"),
    "city" : "Bangalore",
    "zip" : "xxxxx",
    "loc" : {
        "y" : 5.3667,
        "x" : 3.3333
    },
    "state" : "Karnataka"
}

Documents are encapsulated between opening and closing braces, and composed of key-value pairs. Every document has a unique and mandatory “_id�?, a 12 bytes field hexadecimal number.

The first 4 bytes represent the current timestamp, the next 3 bytes for the machine id, next 2 bytes for the process id (mongod) and the remaining 3 are an incremental value. Here we have a second document under the main document, which is an embedded document (there is no “_id�?). All documents are grouped into collections.

Getting that connection to the database

Let’s get around to setting up a connection with the Mongo DB database. Jumping straight into the code let’s do direct connection and then look at the code.

// Retrieve
var MongoClient = require(‘mongodb’).MongoClient;
// Connect to the db
MongoClient.connect(“mongodb://localhost:27017/sampleDb�?, function(err, db) {
 if(!err) {
   console.log(“We are connected�?);
 }
});

Let’s have a quick look at how the connection code works. The Db.connect method lets us use a URL to connect to the Mongo database, where localhost:27017 is the server host and port and sampleDb the db we wish to connect to. After the URL notice the hash containing the auto_reconnect key? This auto reconnect tells the driver to retry sending a command to the server if there is a failure during its execution.

Another useful option you can pass in is poolSize. This allows you to control how many TCP connections are opened in parallel. The default value for this is 5 but you can set it as high as you want. The driver will use a round-robin strategy to dispatch and read from the TCP connection.

We have now setup the connection to the database.

Some basic commands on this shell are as follows:

   1.db.version(); – This shows the current version of MongoDB.

   2.show databases; – This will enlist all the databases in MongoDB. While creating a database or collection in MongoDB, it will check whether the given database or collection is already exists. If it exists, it will dump data in respective database or collection. And if the given database or table doesn’t exists within MongoDB, then it will create it. It will not show any error like in RDBMS.

basic command

  1. Creating Database:
    Syntax: use database_name ;

Example: use myDatabase;
The above command will create a database named ‘myDatabase’, if  it doesn’t exist already. Though you might not be able to see it using the command SHOW DATABASES; , till some collection is created.

  1. Importing Data in MongoDB:

MongoDB comes with mongoimport and mongoexport for importing and exporting data. These commands should be triggered on command prompt.

Syntax: mongoimport --db <destination_database_name> --collection <destination_collection_name> --file <source_file_path>

You need to prepare a Sample Dataset or you can download the JSON test dataset when performing the import/export operation. To being with, extract the zip file and you will have the dataset.json file in the extracted folder.

Next, create a folder in C: drive with the name ‘test’ and movethe dataset.json in this test folder.

Here’s the command to Import the data into MongoDB:

C:\Program Files\mongodb\bin>mongoimport –db myDatabase –collection contacts2 –file C:\test\dataset.json
In the above command, replace C:\test\dataset.json with your actual path of your dataset.json.
import command
On successfully import the data you will see a message like “imported 25359 objects�?

  1. db.getCollectionNames(); – To display the collection.
  2. Exporting Data from MongoDB:

Syntax: mongoexport –db <source_database> –collection <source_collection> –out <destination_file>

  1. Update Database: By default, the update() method updates a single document. You need to set the multi parameterto update all documents that match the query criteria.

Syntax: db.collection.update(query, update, options)

  1. dropDatabase – The ‘dropDatabase’command drops the current database, deleting the associated data files.

The command has the following form:{ dropDatabase: 1 }

Conclusion:

With the rise of agile software development and continuous integration, the structures and data types have evolved to the point of not being in agreement with the basic principles of relational databases. The responsibility of the data schema now goes to the software developer and not to the database management system. Compared to RDBMS classic pattern, this new storage mode allows developers to have more flexibility when it comes to evolving data schemas over time.

I hope you enjoyed this quick introduction to MongoDB. In case of any queries, feel free to write us at support@acadgild.com and keep visiting www.acadgild.com for more updates on the courses.

>