MongoDB tutorials

Learn MongoDB basics from our easy-to-follow tutorials.

Table of content:

  1. Download and Installation.
  2. Insert and read documents.
  3. Query document.
  4. Projected fields, limit, skip, and sort.
  5. Update, increment, and delete documents.
  6. Aggregation and group by clause.
  7. Indexing.
  8. Capped collections (automatically remove previous documents).
  9. Text search.
  10. Deployment (mlab.com)

1. Download and Installation

Video tutorial:

You can download MongoDB from their official site: https://www.mongodb.com/download-center and you need to select “Community server” if you are using it for localhost or small websites. For a very large company, I would recommend “Enterprise server”. Once downloaded, extract the zip file and you will see a folder named “mongodb”. Inside this folder, you will have another folder named “bin”.

Now open your terminal in that folder, you can use the following command:

cd "path to bin folder of mongodb"

Once you are inside the folder, you can start the MongoDB server using following command:

./mongod

If that didn’t work, try simple:

mongod
mongodb start the server

Connect with MongoDB

Once your MongoDB server is started, you can open the database by opening a new command terminal. You can either open a new terminal window (window + n) or new tab (window + t).

window = cmd in Mac OS

And in terminal window or tab, run the following command (make sure the MongoDB server is running from previous command):

./mongo

If that didn’t work, try simple:

mongo
connect database – MongoDB tutorials

Now you are in your database server. Here you can enter any MongoDB command to perform specific actions. By default, MongoDB server creates some databases for administrators and configurations. You can view all databases in the MongoDB server by running the following command:

show dbs
mongodb view all databases

To create a database, you just need to use the “use” command. Use command will create a database if not exists, and open the database if already exists. For example, to create a database for users, run the following command:

use users

In all the tutorials of this MongoDB series, we will be using “users” collection.

Now if you run the command “show dbs“, you will NOT see your “users” database. This is because it will be displayed only when there is at-least one document in it.

We will know more about documents and collections and how to add them in the next tutorial.