Skip to main content

MongoDB: Getting Started With CRUD Operations

MongoDB is No-Sql Open Source database system which uses document as record of database.
MongoDB is designed for high performance and high accessibility.  

Document is set of field => value pair combination and store this information in form of BSON ( BSON is binary representation of JSON records) which is native data type in most of languages.

MongoDB store set of documents in the form of collection.

Now let’s see the basic operations which can be handled in MongoDB.
  •          Create
  •          Read
  •          Update
  •         Delete

Collectively called CRUD operation

Create Operation

MongoDB perform atomic create operation on single document. It adds new document to selected collection.

                 field => value,
                 field => value,
                 field => value,

Here value could be simple datatype or complex (document or array)
Create operation always add unique identifier of ObjectId data type to newly created document.

ObjectId is 12 byte BSON primarily used to maintain uniqueness in collection. Following is byte structure of ObjectId.

  • 4 byte for Unix epoch value
  • 3 byte for machine identifier
  • 2 byte for process identifier
  • 3 byte for some random value


                name : “Zainul”,
                address : "XYZ Address"

Read Operation

Read operation fetches documents from single or selected collection. It uses projection and condition to modify resulting documents.

  •     Project is used to fetch only select fields of document which help to increase read operation.
  •     Condition is used to select particular document which falls under given condition.

           { condition },
           { projection }

We can use limit and sort to limit and order resulting documents

Note:  order of resulting documents is not defined unless we provide order for documents


        { name: ‘Zainul’ },
        { name : 1, email : 1}

This command will return set of documents from user collection whose name matches with given condition and fetch only name and email.

Note: read operation always returns id unless we omit it by using query projection

Update Operation

Update operation modify existing document or even create new document. It uses update criteria to isolate documents and update action to modify values of selected documents.

           { update criteria },
           { update action },
           { update option }

Update option tells MongoDBto update multiple documents.


        { name : 'Zainul' },
        { $set : { name :  "Abedin' } }

Note:  use upsert to create a new document if that document does not exist.

Delete Operation

Delete operation remove single or multiple documents from selected collection. It uses remove criteria to identify the document which is to be removed.

     {remove criteria}

If remove criteria is not defined then it will remove all the documents.



      { name :  'Abedin' }

These basic operation will help to get started with MongoDB CRUD operation.


Popular posts from this blog

AngularJs Tutorial: Getting Started

This tutorial will help you to create a dynamic web application using power of AngularJs. ·   What is AngularJs AngularJs is javascript MVC framework (maintained by Google) which molds static HTML application to dynamic web application. That was a brief introduction, you can find more information on Wiki and AngularJS Site . AngularJs follows MVC pattern to separate out model from view and manage it by controller. See this image Goal of using AngularJs is to convert static HTML template to dynamic (Angular) template.   ·   How to convert static template to dynamic (Angular) Template Now consider the following static HTML template which contain list of movies title. <html> <head></head> <body>   <h1>Upcoming Movies</h1> <ul>     <li>       <span>Godzilla</span>     </li> <li>       <span>Batman vs Superman</span>     </li> <li>       <span>Sta

Abstract Factory design pattern analysis

What is Abstract Factory design pattern?     The intent or definition of Abstract Factory is “ Provide an interface for creating families of related or dependent objects without specifying their concrete classes ” It is kind of an obscure definition, in order to understand it we need to break it into meaningful pieces. 1.  " Provide an interface "  When we talk about Abstract Factory we talk about interface not abstract class , Basically this design pattern gives us an Interface, like Interface in Java, C# , Typescript (abstract class).    Why interface?  Whenever we talk about any design pattern, it emphasizes more on how we are going to use it rather than implement it. Interface is always the right type to prefer because concrete class creates tight coupling whereas an interface isolates it from the rest of the system. At runtime we can get different solution, without updating our code. For example interface SecurityFactory

AngularJs Tutorials : Bootstrap

Every application starts with bootstrap process which initialize application and wire it other with dependencies and configurations. AngularJs is not different from other application. It also starts application with bootstrap process. Following operation happens inside AngularJs bootstrap process. Load application module. Create dependency injector and load dependencies. Compile HTML and create scope for application. All these steps happens inside angular.js scripting file. therefore we need to include it first. we can include it inside HEAD tag or at end of BODY tag. Note: Adding angular.js file at end of body tag will allow browser to load of HTML elements without any delay and afterwards load angular.js and begin bootstrapping process. You can get angular.js source file from <script src= ”” type= ”text/javascript” > AngularJs bootstrap process happens on docum