In this post, we are going to go through some basic concepts of MongoDB and will Perform CRUD operations on console.

What is MongoDB?

MongoDB is an open-source database management system (DBMS). It is based on a document-oriented database. it is a NoSQL database, which means it doesn’t store data in tables and rows as in relational databases. It stores data in JSON-like documents which can be very flexible as we don't have to define the structure, such as the fields or the types of their values. In same collection, documents can have different keys and different data types.

Some Basic Concepts

  • Database: It is a physical container that stores our collections. Single MongoDB Server can have multiple databases.
  • Collection: It is a group of documents. It is similar to the Table concept in RDBMS. The collection doesn't force our documents to have the same keys and same data types as Table does in RDBMS.
  • Document: It is the set of keys and values. It has a flexible schema, which means it can have different keys and data types in the same collection.

Data Types:

Some basic data types in MongoDB

  • String: "hello world." is an example of the string. String in MongoDB must be UTF-8 valid.
  • Integer: It is used to store numeric data.
  • Boolean: It is used to store boolean data, true or false.
  • Arrays: It is used to store a collection of data. Example: ['Hello','world','!']
  • Object: It is used to store embedded data.
  • ObjectId: It is used to store document _id, it is a unique id that is generated by default. you can say the primary key in the document.

Some more data types to explore here.

Example of MongoDB document:

  "_id": ObjectId("5ef0ec62f4347b6a39c856332"), // ObjectId
  "name": "Jignesh Verma", // string
  "age": 16, // integer
  "isStudent": true, // boolean
  "isEmployed": false,
  "favFruits": [ // array
  "totalBankBalance": null, // null
  "dob": ISODate("2014-02-14T10:50:42.389Z"), // date
  "school": { // object
    "name": "Rishi High School",
    "adderess": "Nainital, Uttarakhand"
  "created": 1592847458901.0 // timestamp


You can follow these posts to install in your own system.

  1. MongoDB Docs.
  2. Tech Admin, Installation on Ubuntu



You can create a database by using the use keyword.

Use Keyword

and use the db command to check which database we are currently using.

Db Keyword

let's create our collection.

Create Collection

We can list all databases in our mongo server by using show dbs. Empty Database will not be listed. (if Database has no collection. Then it would be considered as empty.

Show All Databases

Now we can insert a document in users collection.

Insert Document

We have inserted one document, let's insert multiple documents at a same time by using db.collectionName.insertMany([{}]).

Insert Many Document


We can retrieve all documents by find operation.

Find Operation Document

we can also specify query criteria to identify documents in find operation.

Find Operation with query

Here we have filtered users by age, where we found users greater than equal to 11. $gte is a MongoDB operator which matches values that are greater than or equal to a specified value. You can learn more about operators in this Article Mongodb Operator Manual.

We can format or prettify our data by using pretty() function.

Find Operation with pretty data

There is another find query which is similar to the find but there is a predefined limit which is one.

As we can add a limit to the find and skip the number of documents by limit() and skip() function respectively.

The limit() method limits the number of documents in the result documents.

Find Operation with limit data

The skip() method controls the starting point of the results documents. The following operation skips the first 2 documents in the collection and returns all remaining documents

Find Operation with skips data


Modifies an existing document or ducuments in a collection. db.collectionName.update() method can update fields or replace existing document.

To update or create specific fields we can use $set operator.

Update $set MongoDB

We updated the one document where "_id" = ObjectId("5f3a85bba730d6aa35fd3faf") and with the help of $set operator, where we have updated one field age and added new field salary on that document.

we can delete a field by using $unset operator.

Update $unset MongoDB

Here we have removed the salary key from all the documents. The multi true option allows updating all the document which matches the where criteria.

We can also pass upsert true as an option. which tells update the document if it exists else to create. db.users.update({},{$unset:{age:""}},{upsert:true})


MongoDB provides db.collectionName.deleteMany() db.collectionName.deleteOne() two functions to remove documents from collections. To delete all documents db.collectionName.deleteMany({}) pass empty in filter clause.

db.collectionName.deleteOne({}) removes one document which matches the provided criteria.

Delete One MongoDB

db.collectionName.deleteMany({}) removes all the document which matches the provided cirteria.

Delete One MongoDB

{name:{$exists : true }} selects the document which has name field in the collection.


In this post, we have implemented CRUD functionality in MongoDB. Thanks for reading. if you found any issues or you have any query please contact.

Categories: MongodbDatabase