MongoDB Basics Tutorial

MongoDB is an open-source and leading NoSQL database. In this tutorial, we will learn MongoDB basic commands.

Introduction

MongoDB is very popular NoSQL database. MongoDB stores information in a JSON format but not limited to JSON, you can also store other data formats.

Terminologies

Here is the common MongoDB terminolgies.

  • Database – Stores collections
  • Collections – similar to tables, store data.

MongoDB Installation

Head over to the official MongoDB installation page and download the MongoDB installer suited for your operating system.

We can verify the installation of MongoDB by running the MongoDB shell. Open your terminal and type mongo command.

MongoDB Shell

Awesome. Moving right along.

Creating database

To create a new database, just type the following command.

use database_name

Mongo create database

You can run this command to validate whether the database was created or not.

show databases

Let’s create a collection.

Creating collection

To create a new collection, run the following command.

db.createCollection('collection name');

Mongo New collection

You can run this command to validate whether the collection was created or not.

show collections;

Let’s add some data in the collection.

Adding data in the collection

You can use the insert() method to add a new data in the collection.

db.users.insert({ some data});

MongoDB add data

Awesome. Let’s search for the data we just added.

Finding the data in the collection

You can use the find() method to search for the information inside the collection.

db.users.find({})

MongoDB find documents

Updating and Deleting data in the collection

You can use update() and delete() method to update or delete the information in the collection.

Update and delete data from mongo collection

Summary

We learned the basic commands of MongoDB and how to use them to perform operations in MongoDB.

Further Study

Build a RESTful API using Node and MongoDB
Getting Started With MongoDB Atlas with Node.js

Pankaj Kumar
Pankaj Kumar
Articles: 209