What is MongoDB? and Windows Installation

It is a quick tutorial on MongoDB and how you can install it on your Windows OS. We will also learn some basic commands in MongoDB for example, creating and dropping a Database, Creation of a collection and some more operations related to the collection.

What is mongodb and windows installation


Contents

  1. What is MongoDB?
  2. Windows Installation
  3. Commands

 


 

1. What is MongoDB?

MongoDB is an open-source NoSQL database engine written in C++ that provides high performance, high availability, and automatic scaling. It’s a Document-store database which means it stores data as a document inside a collection, with multiple collections inside a database. Multiple databases can exist on a server.


 

2. Windows Installation

What is MongoDB?

You can download MongoDB from its Offical Website, where a couple of options is available. Then we first need to decide what Operating system we want to install that. In this tutorial, we are installing it in Windows so, Download the msi by click on Download Button.

After downloading successfully Double-click on it. MongoDB installer will be open.

MongoDB installer

Follow the installer and click Next. And in Choose Setup Type click on Complete and click on Install Button. After installing successfully, click on Finish Button.

MongoDB automatically it gets stored in program files. You could perform a custom installation and change the directory.

Now MongoDB is on our machine for checking that Open Windows Explorer and navigate to the Program Files folder in C Drive. In Program Files now we have a new directory MongoDB, double click on that and then go to the Server, 3.2 and bin directory. Bin directory contains bunch of MongoDB Executable files

Start MongoDB

In the Bin directory searches for the mongo.exe and mongod.exe files.

mongod.exe

This is an actual database and when we run it then we start using our system as a database. Its runs in the background.

For running it we use Command Line Prompt. S0, open Command Line Prompt and navigate to the bin directory in MongoDB. For executing a .exe file just type its name and press enter.

When we execute it then at the bottom it shows a message Data directory C:\data\db\ not found., terminating. What does means is that you are trying to setup a database to store the bunch of information. For this, you need to give a directory to store all that. Standard location is C:\data\db\ . Just create those directories, now any data we store using MongoDB it goes here.

Now again run mongod command in Command Line Prompt, now our mongod running successfully and is waiting for connection on port 27017.

mongod2

Our Server running successfully, now we can start using commands using mongo.exe. We can’t able to write anything in mongod, it runs in background.

mongo.exe

mongo.exe is a Command line interface application from where we handle our all operations. For running it open Command Line Prompt and navigate to Bin directory in MongoDB and type mongo command and press enter.

For checking type db command and press enter.

mongo


 

3. Commands

Here, are some basic commands –

  • use

Using use command we can create a new database or switch the database (A database is just a collection of data).

Syntax –

use database-name

When we are using use command then it first checks the database-name is available or not if it’s available then switch to it otherwise create a new database with given database name and switch to it.

Example –

For example, you want to create a database name “social” for your project.

> use social
switched to db social
  • db

For finding out in what database we are currently working for this we need to execute db command. It returns the current working database.

Syntax –

db

Example –

> db
social
  • dropDatabase()

For deleting a database and its tables we use dropDatabase() command. It deletes the current database.

Syntax –

db.dropDatabase()

Example –

> db.dropDatabase()
{ "ok"  : 1}
  • collection

The database contains a number of collections and collection is use to store documents.

Syntax –

db.collection-name.insert([data])

MongoDB can create collection implicitly.

Example –

We want to create a collection "users" and store document in it.

> db.users.insert({name:'Yogesh',age:22,city:'Bhopal'})
WriteResult({ "nInserted":1 })
  • find()

For viewing documents in the collection, use find().

Syntax –

db.collection-name.find()

Example –

> db.users.find()
{"_id" : ObjectId("575d0df85f55dd20630e6726,"name" : "Yogesh", "age" : 22, "city" : "Bhopal"}

pretty()

For displaying result in more formatted way you can pretty() function with find()

Syntax –

db.collection-name.find().pretty()

 Example –

> db.users.find().pretty()
{
     "_id" : ObjectId("575d0df85f55dd20630e6726"),
     "name" : "Yogesh",
     "age" : 22,
     "city" : "Bhopal"
}

Selecting particular document

If in collection you have large number of documents and you want to select particular one in that case you can case you can use also find() method.

Syntax –

db.collection-name.find(SELECTIOIN_CRITERIA)

Example –

> db.users.find({name: "Sonarika"}).pretty()
{
    "_id" : ObjectId("575d61105f55dd20630e6727")
    "name" : "Sonarika",
    "age" : 23,    "city" : "Pune"
}
  • Updating collection

    Whenever we insert the document in a collection then MongoDB automatically insert an ObjectId to the document from these we can identify each document uniquily.

    For updating a document in Mongo we use update() method.

 Syntax –

db.collection-name.update(SELECTION_CRITERIA, UPDATED_DATA)

Example –

> db.users.insert({name : 'Anil',age : 24, city : 'Indore'})
WriteResult({"nResulted" : 1})
> db.users.find().pretty()
{
    "_id" : ObjectId("575d6b8e5f55dd20630e6729"),
    "name" : "Anil",
    "age" : 24,
    "city" : "Indore"
}
> db.users.update({"_id" : ObjectId("575d6b8e5f55dd20630e6729")},{name : 'Anil', age : 22, city : 'Indore'})
WriteResult({"nMatched" : 1, "nUpserted" : ), "nModified" : 1})
> db.users.find.pretty()
{
    "_id" : ObjectId("575d6b8e5f55dd20630e6729"),
    "name" : "Anil",
    "age" : 22,
    "city" : "Indore"
}
  • Remove document

    For removing documents from collection we use remove() method. In remove method we pass SELECTION_CRITERIA if we want to remove document based on condition otherwise pass empty CRITERIA.

Syntax-

db.collection-name.remove({})    // For removing all documents
db.collection-name.remove(SELECTION_CRITERIA)   // based on condition

Example –

> db.users.remove({name:'Vishal'})

WriteResult({ "nRemoved" : 2  })

Here, we removed documents whose name is “Vishal”.

>db.users.remove({})

WriteResult({ "nRemoved" : 3 })
  • Drop collection

    drop() method is use to delect a collection

Syntax –

db.collection-name.drop()

Example –

> db.users.drop()
true

 

Leave a Comment