MongoDB CRUD Tutorial With Example From Scratch

MongoDB CRUD Tutorial With Example From Scratch is the today’s main topic. CRUD operations refer to the primary Insert(Create), Read, Update and Delete(Destroy) operations. NoSQL Database, as many of you may already know, is a database used to manage huge sets of unstructured data where the data is not stored in tabular relations like relational databases.

There are the following NoSQL database datatypes.

  1. Document Databases
  2. Graph stores
  3. Key-Value stores
  4. Wide-column stores

MongoDB Database

MongoDB is a NoSQL database which stores the data in the form of key-value pairs. It is an Open SourceDocument-Oriented, Scalable Database.

In MongoDB Overall structure, At the upper level, there is database name, then different Collections and then finally Documents.

So if we compare it to the MySQL then database name is same for both, then one step down the level in MySQL, there is the table, wherein NoSQL MongoDB it is Collections. Finally, In MySQL there are Columns, and in MongoDB, there are Documents.

Install MongoDB

If you have installed MongoDB database on Mac previously, then skip this step. For new beginners, 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, you need to create the “db” directory. The db folder is where all the Mongo data files will live. You can create the folder in the default location by running mkdir -p /data/db.

Please make sure that the /data/db directory has the all correct permissions. If not, then please give the permission by the 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

 

MongoDB CRUD Tutorial With Example From Scratch

Now, your MongoDB server is running and ready to connect to any client. You need to open this terminal and do not close the terminal. Otherwise, the connection between a web application or client and database will be lost.

MongoShell Interface

The mongo shell is an interactive JavaScript shell interface to MongoDB. It provides a powerful CMD interface for systems administrators as well as a way for developers to test queries and operations directly with the database. We can almost do all the database related activity through this shell. You do not need any GUI based interface for that. If you have a sharp grip on CMD, then you will gonna like it.

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

show dbs

MongoDB CRUD Tutorial

Now, we will create a database and perform a crud operation on that.

Create Database.

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

use demo

It will switch to the demo database, and if the database is not there, then it will create for you.

If you want to check your currently selected database, use the following command.

db

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 establishes that collection on the fly.

db.person.insert({name: 'gilo', age: 30})

Now, you will see something like this.

WriteResult({ "nInserted" : 1 })

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

Batch Insert in MongoDB

The Batch Insert is an operation that allows storing of multiple documents in a database at a time. You can send the hundreds or thousands of documents in a batch at a time. This feature makes the whole insert operation faster than regular insertions as multiple documents or records can be inserted simultaneously.

Ordered Bulk Insert

In the ordered operations list, MongoDB database performs the write operations in the list serially. If at any particular point of time, any error occurs during one of the write operations, MongoDB returns without executing the remaining write operations in the list. It will not continue from that.

Unordered Bulk Insert

In an unordered operations list, MongoDB database can perform the write operations in parallel in a random order. If at any point of time, an error occurs during the write operation, MongoDB will perform remaining write operations uninterrupted. When performing an unordered list of operations, MongoDB groups them.

Read Collection

You can see the created document using the following command.

db.person.find();

 

MongoDB Pretty Collection

I have used pretty() to look results decidedly more excellent.

You can observe that the record retrieved contains an attribute called _id with some unique identifier value called ObjectId. It acts as a document identifier.

Okay, now add two more documents and Read that documents.

 

MongoDB Retrive Data

Update Collection

Now, if you want to update any collection, then you should specify the criteria on which you need to update the particular document. You can write the update query like this.

db.person.update({"name": "wade"}, { $set: {"age": "35"} })

So, we have updated age, whose name is wade.

We can see the changes using the following command.

db.person.find().pretty();

 

MongoDB database update tutorial

Delete Collection

We can delete the collection based on the provided criteria.

db.person.remove({"name": "gilo"});

 

MongoDB Delete Tutorial

Drop Database

We can drop the database using the following command.

db.dropDatabase()

MongoDB GUI

If you do not like the Command Line Interface, then you can also use Studio3T for the GUI based MongoDB implementation. It is very helpful.

If you want to do some practice using MongoDB, then you can do it here.

MongoDB GUI Support

 

At last, MongoDB CRUD Tutorial With Example From Scratch is over.

Tags:

Add a Comment

Your email address will not be published. Required fields are marked *

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