Home » Database, MongoDB27 June 2017

MongoDB: Basic Select, Insert, Update, Delete – CRUD [Beginner Tutorial]

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:

To stop and restart MongoDB server:

Go to MongoDB shell

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

Show databases

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.

db command will show the currently selected database name.

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

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.

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

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

Drop database

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

We create a new database named “hello”

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

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:

Now, we drop the database.

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

Drop Collection

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

Insert Document

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

Query Document

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

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

Querying document for individual name value [where name = ‘Mukesh’] [where name = ‘Chapagain’]

Update Document

update() and save() methods can be used to update document data.

db.COLLECTION_NAME.update(SELECTION_CRITERIA, UPDATED_DATA)

Here, we update the name “Mukesh” to “PHP”:

db.COLLECTION_NAME.save({_id:ObjectId(),NEW_DATA})

Here, we update data by ID value. We change the name from “Chapagain” to “Magento”.

Delete Document

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

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

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.

db.COLLECTION_NAME.find({},{FIELD_NAME:1})

We can also set “FIELD_NAME:0” to deselect it.

Here, we deselect id and select only name field from the document.

Bulk Insert

Insert more than one document at once.

Limit & Sort

>db.COLLECTION_NAME.find().limit(NUMBER)

>db.COLLECTION_NAME.find().sort({FIELD_NAME:1})

Pretty Print the Output

pretty() function makes the output more readable.

Hope this helps. Thanks.

MongoDB

Get New Post by Email

Find me on

FacebookTwitterGoogle+LinkedInRSS Feed