Getting Started with MongoDB on Windows

By July 16, 2019 S.E

A guide to getting started with MongoDB, a NoSQL database program, on windows.

Installation

The first step, obviously, is installation. Visit mongodb’s official download center to find a version compatible with your operating system and computer. I’m using Windows for this article.

Create required directories

MongoDB needs directories to store logs and database files.

In the C drive, create two directories:

mkdir c:\data\db

mkdir c:\data\log

 

Start MongoDB

After a successful installation, run mongod.exe from the cmd (command prompt) to start MongoDB by entering

`”C:\Program Files\MongoDB\Server\4.0\bin\mongod.exe”`

This commands starts the main database processes.

 

The bin folder has two executable files for:

mongod.exe and mongo.exe

The first is responsible for managing all MongoDB background processes while the second is a command line shell that allows users to interact with the database.

Connect to MongoDB

To begin using MongoDB, open another terminal and run:

“C:\Program Files\MongoDB\Server\4.0\bin\mongo.exe”

Don’t forget to add the quotes

Set up environment variables

Now, you don’t need to type an entire path every time you intend to start MongoDB. Setting up an environment variable shortens this to just `mongo`.

On Windows (which is what this article is for), head over to

Advanced System Setting –> Environment Variables –> New

Under Variable Name enter the name you want to give the environment variable; preferably something simple like Mongo. Under Variable Value, enter the path ‘C:\Program Files\MongoDB\Server\4.0\bin’. This path has to be exactly where you have MongoDB installed on your computer.

Using MongoDB

Check what database you’re currently on by running;

db

It should be the test database.

To list the databases available, run;

show database

To create a new database;

Unlike in SQL, MongoDB does not work with the CREATE DATABASE command. Things are done a little differently here.

A database is created by running

use <dbname>

If the database does not already exist, it will be created.

However, the database is not technically created until you insert data into it. When you run show databases you will not see your newly created database.

 

Insert a document into a collection (insert data)

Documents consist of a {name: ‘value’} pair. Documents are like data in a database.

db.collectionName.insert({ name: "value" })

 

This command creates a collection with data in the new database.

Note that I have used my own example

Now you’ll be able to see your new db when you run show databases.

 

 

 

View the contents of your database

db.<collection-name>.find()

Create collections

You can create collections using the createCollection() method:

db.createCollection(<collection-name>)

Delete a collection

db.collection-name.drop()

Delete a database

db.dropDatabase

 

These are very simple basics to using MongoDB. There’s a ton more you can do and learn about it from its documentation.

 

 

Comments

comments

Lulu Ngei

Lulu Ngei

Live a little Love a little Laugh a little