AppDividend
Latest Code Tutorials

NoSQL MongoDB Tutorial With Example From Scratch

498

Get real time updates directly on you device, subscribe now.

NoSQL MongoDB Tutorial With Example From Scratch is today’s topic. MongoDB is an open-source document database and leading NoSQL database. MongoDB is a cross-platform, document-oriented database that provides, high performance, high availability, and easy scalability. MongoDB works on the concept of collection and document. It is not Relational Database like MySQL or Oracle. In MySQL database, there is Database then Tables and then Columns. In MongoDB structure is like Database then Collections and then Documents. So there is no concept like Tables and Columns in the MongoDBMongoDB database is vastly popular with Node.js platform. 

NoSQL MongoDB Tutorial

First, we learn the basic structure of MongoDB database like what is collections and what is a document. Then we will install MongoDB on Mac System and then play with it.

Collections

A collection is a group of MongoDB documents. It is the equivalent of a Relational Database table. A collection exists within a single database.

Document

The document is a set of key-value pairs. Documents have a dynamic schema. Dynamic schema means that documents in the same collection do not need to have the same set of fields or structure, and standard fields in a collection’s documents may hold different types of data.

Example of Document

{
	"_id" : ObjectId("5ac0ac19d412d208b8c91214"),
	"course_name" : "react.js",
	"course_price" : 10,
	"__v" : 0
}

It is like JSON Object. The document contains key-value pairs. Here, _id is generated by MongoDB. It is equivalent to id in MySQL database; the values are different because in most case in MySQL id is incremental field starting from 1.

Install MongoDB on Mac.

If you have installed MongoDB database on Mac, then skip this step. For newbies, go to your terminal and hit the following command to install homebrew. If you have installed, then do not type this command.

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Next, add Homebrew’s location to your $PATH  in your .bash_profile or .zshrc file.

export PATH="/usr/local/bin:$PATH"

Now, install MongoDB using Homebrew.

Type the following command in your terminal.

brew install mongodb

After downloading Mongo, create the “db” directory. This is where the Mongo data files will live. You can create the directory in the default location by running mkdir -p /data/db

Make sure that the /data/db directory has the right permissions by running the following command.

sudo chown -R `id -un` /data/db

Run the Mongo daemon, in one of your terminal windows run mongod. It should start the Mongo server at port number 27017.

mongod

Install MongoDB on Mac

Now, your MongoDB server is ready to connect any web application. Once you are connected, you can see here that one connection is added. While you are working with Any web project that needs to use MongoDB database, you need to open this connection, do not ever close terminal. Otherwise, the connection between a web application and database will be lost. So never down mongo server.

MongoShell Interface

mongo is an interactive JavaScript shell interface to MongoDB, which provides a powerful interface for systems administrators as well as a way for developers to test queries and operations directly with the database. 

Now, if we want to see how many databases are there by default, then we can use the following command.

show dbs

MongoDB Database Example

Create Database.

Related Posts
1 of 10

Now, we can create one database using the following command.

use demodb

If the database is not there, then it will create for us. If already there, then it switches to that database.

To check your currently selected database, use the following command.

db

MongoDB Tutorial

Create Collection.

The cool thing about MongoDB is that you need not create a collection before you insert the document into it. With a single command, you can add a document in the collection, and the MongoDB creates that collection on the fly.

db.person.insert({name: 'mahavir', age: 70})

It will create a collection called person and add one document in it.

So, you can see the inserted documents using the following command.

db.person.find()

For a prettier result, we can query like this.

db.person.find().pretty()

Beginners guide to up and running with MongoDB

Shell Help

To see the list of help, in the mongo shell type the following.

help

It will list the commands. You can use it to find help in database administration, query the results and other stuff.

mongodb help

Now, let us say, we need help to find all the documents in the person collection then we can go through help to execute the right query.

From starting, first, check whether you are in the right database or not. So first switch to a right database.

use demodb

Then, check whether you are in the collection or not.

db

If not then switch to the right collection using the following command.

use person

Now, type the following code to get all the methods that can apply to any collection.

db.person.help()

Database collection help

Now, you can apply the first command that will return all the documents. But you can go further for help. Type the following command.

db.person.find().help()

So, you will see what other methods you can chain with find() method to get the desired result.

MongoDB NoSQL Database Tutorial

You can also drop the database using the following command.

db.dropDatabase()

We will deep dive into NoSQL MongoDB database in future posts. That is it, for now, NoSQL MongoDB Tutorial is over. Thanks for taking.

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.