MongoDB is an open-source document database. Document in MongoDB means a row or record in the database. A document contains data in key-value pairs. MongoDB is popular for it’s high performance, scalability and availability.

This article shows how to perform simple select, insert, update, delete database operations in MongoDB. That’s also referred as CRUD (Create, Read, Update, Delete) operation. We will be performing these operations through terminal/command-prompt using MongoDB shell.

I assume that you have already installed MongoDB on your computer. Here’s the installation guide if your have not installed it already: MongoDB Installation

To start MongoDB server and to check the status of MongoDB server, you can run the following commands on your terminal:

1

2

sudo service mongod start

sudo service mongod status

To stop and restart MongoDB server:

1

2

sudo service mongod stop

sudo service mongod restart

Go to MongoDB shell

In your terminal/command prompt, run the command mongo:

1

2

3

4

5

6

mukesh@chapagain:~$mongo

MongoDB shell version v3.4.4

connecting to:mongodb://127.0.0.1:27017

MongoDB server version:3.4.4

Welcome tothe MongoDB shell.

Forinteractive help,type"help".

Show databases

1

2

3

>show dbs

admin0.000GB

local0.000GB

Create new database

use is used to create new database in MongoDB if the database does not exist. It then selects/switches to that database. If the database already exists then this command will simply switch to the database.

Let’s create a new database named test.

1

2

>usetest

switched todb test

db command will show the currently selected database name.

1

2

>db

test

Now, let us use show dbs command to see if it shows our newly created database or not:

1

2

3

>show dbs

admin0.000GB

local0.000GB

It is still not showing the database named test that we recently created.

Actually, to show the database, the newly created database should have some data in it.

Create Collection

MongoDB vs Relational database

Collection = Table
Document = Row
Column = Field

Let’s insert some data into our database.

1

2

>db.users.insert({"name":"Mukesh"});

WriteResult({"nInserted":1})

With the above command, MongoDB will automatically create a new collection named users and insert a document with field “name”.

In Relational database (RDBMS) terms, we call it like creating a new table “users” and inserting a row that has column named “name”.

Show collections created

1

2

>show collections

users

We can also manually create collections using command db.createCollection(name, options)

1

2

3

4

5

>db.createCollection('subjects')

{"ok":1}

>show collections

subjects

users

Drop database

In this example, we will create a new database, then add some data into it and finally we drop that database.

1

2

3

4

>show dbs

admin0.000GB

local0.000GB

test0.000GB

We create a new database named “hello”

1

2

3

4

5

6

>usehello

switched todb hello

>show dbs

admin0.000GB

local0.000GB

test0.000GB

Then we add a collection named “hi” to database “hello”

1

2

3

4

5

6

7

8

9

>db.hi.insert({"name":"mukesh chapagain"})

WriteResult({"nInserted":1})

>show collections

hi

>show dbs

admin0.000GB

hello0.000GB

local0.000GB

test0.000GB

The database is shown with “show dbs” command after adding collection to it.

To check on which database we are, we use “db” command as shown below:

1

2

>db

hello

Now, we drop the database.

1

2

3

4

5

6

>db.dropDatabase()

{"dropped":"hello","ok":1}

>show dbs

admin0.000GB

local0.000GB

test0.000GB

Let’s switch back to database named “test”.

1

2

3

4

>usetest

switched todb test

>db

test

Drop Collection

Here, we drop a collection named subjects from database test. For this db.COLLECTION_NAME.drop() command is used.

1

2

3

4

5

6

7

8

9

>db

test

>show collections

subjects

users

>db.subjects.drop()

true

>show collections

users

Insert Document

db.COLLECTION_NAME.insert(document) OR db.COLLECTION_NAME.save(document) command is used to insert document data into a collection.

1

2

>db.users.insert({name:"Chapagain"})

WriteResult({"nInserted":1})

Query Document

find()findOne() method will fetch only one document.

1

2

3

4

5

>db.users.find()

{"_id":ObjectId("5925a23fa971eb3857cf025c"),"name":"Mukesh"}

{"_id":ObjectId("5925b9faa971eb3857cf025f"),"name":"Chapagain"}

>db.users.findOne()

{"_id":ObjectId("5925a23fa971eb3857cf025c"),"name":"Mukesh"}

You can also use pretty() function after find() function. It will pretty print the result.

Delete Document

db.COLLECTION_NAME.remove({}) = Remove all documentsdb.COLLECTION_NAME.remove(CRITERIA) = Remove only those documents that meet the CRITERIA specifieddb.COLLECTION_NAME.remove(CRITERIA, 1) = Remove only one document that meet the CRITERIA specified

Here, we delete the document that has name “Magento”.

1

2

3

4

>db.users.remove({"name":"Magento"})

WriteResult({"nRemoved":1})

>db.users.find()

{"_id":ObjectId("5925a23fa971eb3857cf025c"),"name":"PHP"}

Projection

Projection in MongoDB means fetching only particular fields of a document, instead of fetching all the fields of the document. We will be using find() command with extra parameter.

About

Mukesh Chapagain is a graduate of Kathmandu University (Dhulikhel, Nepal) from where he holds a Masters degree in Computer Engineering. Mukesh is a passionate web developer who has keen interest in open source technologies, programming & blogging. more...