Skip to content
  • Facebook
GeekCer Logo

GeekCer

The geek's Coding education and Review centre

  • Home
  • Tutorials
    • Java
    • Servlet
    • JSP
    • Python
    • C Tutorial
    • Spring
    • Spring Boot
    • MongoDB
    • Hibernate
    • Data Structure
  • General Knowledge
  • Biography
  • Grammar
  • Festival (त्योहार)
  • Interview
  • Differences
  • Important
  • Toggle search form

Home » MongoDB » CRUD Operations in MongoDB | NoSQL | CRUD Transaction

  • Christmas Day Celebration
    Christmas Day Celebration | Story about Christmas day Festival
  • Chhath Puja Story
    Chhath Puja History : क्यों मनाते हैं छठ महापर्व Festival
  • What is Noun
    What is a Noun? 5 Types of Nouns with Examples, Noun diagram Grammar
  • National Doctors Day in India and Other Countries, July 1, 2022
    National Doctors Day in India and Other Countries, July 1, 2022 General Knowledge
  • Republic day गणतंत्र दिवस | Happy Republic Day
    Republic day गणतंत्र दिवस कब और क्यों मनाया जाता है? Festival
  • Impeachment meaning in Hindi | महाभियोग की परिभाषा और प्रक्रिया
    Impeachment meaning in Hindi | महाभियोग की परिभाषा और प्रक्रिया General Knowledge
  • Jagannath Rath Yatra History in Hindi | जगन्नाथ पुरी की कहानी
    Jagannath Rath Yatra History in Hindi | जगन्नाथ पुरी की कहानी Festival
  • Unicef day
    Unicef day is celebrated on December 11 | Speech on unicef day General Knowledge

CRUD Operations in MongoDB | NoSQL | CRUD Transaction

Posted on December 1, 2021December 1, 2021 By GeekCer Education No Comments on CRUD Operations in MongoDB | NoSQL | CRUD Transaction
CRUD Operations in MongoDB | NoSQL | CRUD Transaction

The most significant element of MongoDB is the CRUD operations. CRUD stands for Create, Read, Update, and Delete, as you may know. In this post, we’ll go through how to insert a document in MongoDB, view a document in MongoDB, update a document in MongoDB, and remove a document in MongoDB.

Before reading this article we would suggest you to get basic information about Documents, Collections, Installation of MongoDB and difference between RDBMS and MongoDB.

Click here to learn the basics of MongoDB, Installation, MongoDB data types

Table of Contents

  • Insert documents in MongoDB
    • Insert document using insert() method in MongoDB
    • Insert document using save() method in MongoDB
  • View Documents in MongoDB
  • Insert Multiple Documents in MongoDB
    • Syntax to Insert Multiple Documents
    • Example for inserting multiple documents in MongoDB
  • Update Document in MongoDB
  • Delete Documents in MongoDB
    • Syntax for Deleting documents in MongoDB
    • Deleting only one Document
    • Delete all documents that meet a set of criteria
    • Deleting all Documents
  • Query Document in MongoDB
    • Retrieve two Documents
  • Count documents in MongoDB
  • Document sorting in MongoDB
    • Sorting in Ascending Order
    • Sorting in Descending Order
  • Skip Documents in MongoDB
    • Example for Skipping documents in MongoDB

Insert documents in MongoDB

The insert operation core part of CRUD Operations in MongoDB. MongoDB has insert() or save() methods that you can use if you want to insert documents into a collection. The following is the syntax:

Insert document using insert() method in MongoDB

To insert a document into the database, use the insert() method. If the collection does not currently exist, the insert operation will create the collection.

> db.<collection_name>.insert(document)

There are examples of insert document using insert() method in MongoDB which are as follows:

> db.dept.insert({"deptno" : 10, "dname" : "ACCOUNTING", "loc" : "NEW YORK"})

> db.dept.insert({"deptno" : 20, "dname" : "RESEARCH", "loc" : "DALLAS"})

> db.dept.insert({"deptno" : 30, "dname" : "SALES", "loc" : "CHICAGO"})

> db.dept.insert({"deptno" : 40, "dname" : "OPERATIONS", "loc" : "BOSTON"})
Insert document using insert() method in MongoDB | CRUD Operations in MongoDB
Insert document using insert() method in MongoDB

Insert document using save() method in MongoDB

As you can see from the syntax and example of the insert() function above. You can also use the save() method to insert a document into MongoDB in addition to insert().

> db.dept.save({"deptno" : 10, "dname" : "ACCOUNTING", "loc" : "NEW YORK"})

> db.dept.save({"deptno" : 20, "dname" : "RESEARCH", "loc" : "DALLAS"})

> db.dept.save({"deptno" : 30, "dname" : "SALES", "loc" : "CHICAGO"})

> db.dept.save({"deptno" : 40, "dname" : "OPERATIONS", "loc" : "BOSTON"})
Insert document using save() method in MongoDB
Insert document using save() method in MongoDB

View Documents in MongoDB

The view operation is an important aspect of any database since it allows us to retrieve information from the database by using a query. The find() method in MongoDB is used to view a collection’s records.

> db.<collection_name>.find()
View Documents in MongoDB | CRUD Operations in MongoDB
View Documents in MongoDB

Insert Multiple Documents in MongoDB

If you wish to store a large amount of data in a database. In other words, if you need to insert more than one record at a time, mongodb has a capability that lets you to do so.

Syntax to Insert Multiple Documents

Here’s an syntax of inserting multiple documents at the same time.

> db.dept.insert([
  { Document 1... },
  { Document 2... },
  { Document 3... },
  { Document 4... }
])

Example for inserting multiple documents in MongoDB

> db.dept.insert([
   {"deptno" : 10, "dname" : "ACCOUNTING", "loc" : "NEW YORK"},
  {"deptno" : 20, "dname" : "RESEARCH", "loc" : "DALLAS"},
  {"deptno" : 30, "dname" : "SALES", "loc" : "CHICAGO"},
  {"deptno" : 40, "dname" : "OPERATIONS", "loc" : "BOSTON"}
])
Insert Multiple Documents in MongoDB
Insert Multiple Documents in MongoDB

Update Document in MongoDB

To update a document in a collection in MongoDB, use the update() and save() methods. To update a document, use the following syntax.

> db.<collection_name>.insert(selection_criteria, updated_data)

> db.<collection_name>.save({_id:ObjectId(), new_data})

The update() and save() functions are used to update a document in the following manner:

> db.dept.update({'deptno': 10},{$set:{'loc':'KOLKATA'}})

> db.dept.save({"_id" : ObjectId("60505578791820eac9f650b2"), "deptno" : 20, "dname" : "RESEARCH",'loc':'MUMBAI'})
The main key _id should be included if you're using the save() method.
Update document in MongoDB | CRUD Operations in MongoDB
Update document in MongoDB

Delete Documents in MongoDB

The remove() method in MongoDB is used to delete documents from a collection. There are two arguments to the delete() function, both of which are optional. The two arguments are listed below:

  • deletion criteria: Here we use the criteria to delete the document. Deletion criteria is nothing but a kind of condition.
  • justOne:  You must use true or 1 if you want to delete only one document in the criteria.

Syntax for Deleting documents in MongoDB

> db.<collection_name>.remove(deletion_criteria)

> db.<collection_name>.remove(deletion_criteria, 1)

> db.<collection_name>.remove({})

Deleting only one Document

> db.dept.remove({ deptno : 40 }, 1)
Removing only one Document
Removing only one Document

Delete all documents that meet a set of criteria

When you need to delete all documents with some criteria you can use it in following way. You have to use remove() method in which you need to specify criteria.

> db.dept.remove({ loc : "BOSTON" })
Remove all documents that meet a set of criteria
Remove all documents that meet a set of criteria

Deleting all Documents

Sometimes you may need to remove all documents at once, then in MongoDB you can use remove() method with empty parentheses. Here’s an example of deleting all documents.

> db.dept.remove({})
Removing all Documents
Removing all Documents

Query Document in MongoDB

The find() method in MongoDB is used to get all documents from a collection.

> db.<collection_name>.find()
Query Document in MongoDB | CRUD Operations in MongoDB
Query Document in MongoDB

limit(<number>) is used with the find() method to obtain a specific number of documents from a MongoDB collection.

> db.<collection_name>.find().limit(<number>)

Retrieve two Documents

> db.dept.find().limit(2)
Retrieve two Documents
Retrieve two Documents

Count documents in MongoDB

> db.<collection_name>.count()

> db.dept.count()
Count documents in MongoDB
Count documents in MongoDB

Document sorting in MongoDB

The sort() method in MongoDB is used to sort documents. If you want to sort in ascending order you have to use 1 and if you want to sort in descending order you have to use -1. We will see in the example below.

Sorting in Ascending Order

In order to sort in ascending order, you will need to use the following syntax.

> db.<collection_name>.find().sort({KEY : 1})
> db.<collection_name>.find().sort({deptno : 1})
Sorting in Ascending Order
Sorting in Ascending Order

Sorting in Descending Order

In order to sort in descending order, you will need to use the following syntax.

> db.<collection_name>.find().sort({KEY : -1})
> db.<collection_name>.find().sort({deptno : -1})
Sorting in Descending Order
Sorting in Descending Order

Skip Documents in MongoDB

The skip(<number>) method in MongoDB is used to skip a document. We use skip() method in combination with the find() and limit() methods.

> db.<collection_name>.find().skip(<number>)
> db.<collection_name>.find().limit().skip(<number>)

Example for Skipping documents in MongoDB

We’ve given you five examples that show you how to skip documents.

> db.dept.find().skip(1)

> db.dept.find().skip(2)

> db.dept.find().skip(3)

> db.dept.find().skip(5)

> db.dept.find().limit().skip(2)
Skip Documents in MongoDB | CRUD Operations in MongoDB
Skip Documents in MongoDB

Hope you liked this article. In this article we have covered almost all the basic functions like Insert, Update, View, Delete, Count etc. This may help you to understand CRUD operations in MongoDB

If you have any doubts related to MongoDB documents and CRUD operations, you can comment here or you can contact us on contact page.

Share this:

  • Click to share on Facebook (Opens in new window)
  • Click to share on WhatsApp (Opens in new window)
  • Click to share on Twitter (Opens in new window)
  • More
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Pinterest (Opens in new window)

Also Read

MongoDB Tags:How do I insert one document into MongoDB?, How to insert data in mongodb?, What are the CRUD operations in MongoDB?, What are the four CRUD operations?, What is the Insert command in MongoDB?, Which method is used for creation in CRUD operations of MongoDB?

Post navigation

Previous Post: Learn MongoDB Tutorial, Installation, MongoDB DataTypes
Next Post: How to create Database and Collection in MongoDB?

More Related Articles

How to create Database and Collection in MongoDB How to create Database and Collection in MongoDB? MongoDB
Learn MongoDB Tutorial, Installation, MongoDB DataTypes Learn MongoDB Tutorial, Installation, MongoDB DataTypes MongoDB

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • National Farmers Day in Hindi | राष्ट्रीय किसान दिवस पर निबंध | चौधरी चरण सिंह जयंती
  • Human rights day in Hindi: 10 दिसंबर ह्यूमन राइट्स डे
  • Unicef day is celebrated on December 11 | Speech on unicef day
  • Indian Navy Day: जल सेना दिवस कब और क्यों मनाया जाता है?
  • P V Sindhu Biography in Hindi, Badminton, State, Caste पी. वी. सिंधु जीवन परिचय, कहानी, राज्य, जाति
  • Draupadi Murmu Biography In Hindi | द्रौपदी मुर्मू की जीवनी
  • Network kya hai (नेटवर्क क्या है)
    Network kya hai (नेटवर्क क्या है) Networking
  • OSI Model | 7 Layers of OSI Model in Computer network
    OSI Model | 7 Layers of OSI Model in Computer network, Functions Networking
  • TCP/IP Model, Full Form, Layers and their Functions
    TCP/IP Model, Full Form, Layers and their Functions Networking
  • Difference between Internet and Intranet
    Difference between Internet and Intranet Differences
  • Difference between TCP and UDP
    Difference between TCP and UDP | TCP vs UDP examples Differences
  • Similarities and difference between OSI and TCP/IP model
    OSI vs TCP/IP Model, Similarities and difference between OSI and TCP/IP model Networking
  • IPv4 Vs IPv6 | Difference between IPv4 and IPv6
    IPv4 Vs IPv6 | Difference between IPv4 and IPv6 Differences
  • Java Tutorial
  • Servlet Tutorial
  • JSP Tutorial
  • Maven Tutorial
  • HTML Tutorial
  • Programs
  • Hindi/English Grammar
  • Difference Between ... and ...
  • HR Interview
  • Important Articles

Write to Us:
geekcer.code@gmail.com

  • About Us
  • Privacy and Policy
  • Disclaimer
  • Contact Us
  • Sitemap

Copyright © GeekCer 2022 All Rights reserved