MongoDB

MongoDB CRUD Operations (Create, Read, Update, Delete)

MongoDB Basic Of Mongo Shell Create read update delete quries.

In the previous tutorials, we have given the introduction to the database and discussed the difference between SQL and NoSQL database. We have also given the brief overview about MongoDB and seen that how we can install MongoDB in Windows operating system. Now in this tutorial, we will learn the basic of Mango shell and introduce some basic MongoDB CRUD Operations.

MongoDB CRUD Operations

let’s go ahead and open the two command prompt in your windows and run command “mongod” in command prompt window. It’s a MongoDB daemon, it will run the server in the background. In the next command, prompt windows run the command “mongo” it will run the MongoDB in the shell.

Help Command:

Now we are going to discuss the command help, help will give us a list of some of the basic features of Mongo. You can also notice that it will give us the list of many commands which show below:

MongoDB CRUD Operations: MongoDB Help Command

You can type ‘show dbs’ if you want to see the database name or if you want to see collections in our current database then we can type ‘show collections’. Here is the demonstration below:

Show Collections MongoDB

Use Command:

The database works in a similar way as compared to other databases, we need to create the database for every single application or every application we work on. We create the database in the same way that we use the database. Here is how we can create a database:

Create Database MongoDB Nodejs

You can notice that we have created a database with name ‘geeksread’ and then our database switched to db geeksread. Currently, our ‘geeksread’ db is empty. Now we are going to insert items into the database.

Insert Command:

Now we are going to add things in the database by creating collections. Now we are using ‘posts’ for geeksread database. So we are going to have collections of ‘posts’ and each post can have things like the title and the content. So we are going to add a single post into our database.

> db.posts.insert({title: "post1", content: "this is post 1"})

Here ‘db’ refers to the database that we are using which is ‘geeksread’ while the posts are the collection and the insert is the command that will insert the data into the posts collection in the geeksread database. Here we have inserted some object into our database which is title and content.

Insert data, MongoDB database

We have got the message that says writeResult inserted 1. To check if the posts collection inserted into the database I can simply run the command ‘show collections.

MongoDB database Show Collections

Find Command:

If we want to check all the posts collections which are available in our database ‘geeksread’, then we need to use find command and find works like this way:

db.posts.find()

Find Items MongoDB database

If we do not pass any arguments with find method then we use to get back everything that is available in that posts collections. You can notice that we have got an object id which consists of the bunch of numbers and letters. This id is automatically assigned by Mongo and they are all unique.

If we add another item in the database and then run the find() method, then we get two items in the collections posts and each item will have unique object id.

find items mongodb

Update Command:

Now let see that how we can update our database if we want to add the category to each posts collection. We can do this way.

db.posts.update({title: "post1"}, {$set: {category: "Mongo"}})

Now if we want to add another category to post2 then we can do this way:

Update items MongoDB database

If we add two more items into the database and then recall by category then we can do this way:

Update items MongoDB database.

Now we have 4 items in our database, if we want to call each item by its category then we can simply achieve by simply passing the category as an object. here is the illustration below:

how TO find the items from collections of MongoDB database

You can notice that we have retrieved every item with category name Mongo.

Remove Command:

Now if we want to remove any item from our database then we can remove by simple writing the remove method and pass in the item that we want to remove as an argument. Here is the demonstration below:

db.posts.remove({title: "post3"})

List down all the items of database, MongoDB

You can notice that we have only three posts left. If we call the delete method and pass the category “Node”, it will remove all the item from the database with category “Node”. Here is the demonstration below:

Removing Item from MongoDB database,

You can notice there that it has removed two items and now we have only 1 item left in our database.

Conclusion:

In this MongoDB tutorial, we have discussed how we can run Mongo daemon, and run the Mongo in the shell. We have also discussed how we can create and show database in MongoDB then we have discussed MongoDB CRUD Operations command which includes create, read, update and delete.

Click to comment

Most Popular

To Top