• Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard
CSEstack

What do you want to Learn Today?

  • Programming
    • Tutorial- C/C++
    • Tutorial- Django
    • Tutorial- Git
    • Tutorial- HTML & CSS
    • Tutorial- Java
    • Tutorial- MySQL
    • Tutorial- Python
    • Competitive Coding Challenges
  • CSE Subject
    • (CD) Compiler Design
    • (CN) Computer Network
    • (COA) Computer Organization & Architecture
    • (DBMS) Database Management System
    • (DS) Data Structure
    • (OS) Operating System
    • (ToA) Theory of Automata
    • (WT) Web Technology
  • Interview Questions
    • Interview Questions- Company Wise
    • Interview Questions- Coding Round
    • Interview Questions- Python
    • Interview Questions- REST API
    • Interview Questions- Web Scraping
    • Interview Questions- HR Round
    • Aptitude Preparation Guide
  • GATE 2022
  • Linux
  • Trend
    • Full Stack Development
    • Artificial Intelligence (AI)
    • BigData
    • Cloud Computing
    • Machine Learning (ML)
  • Write for Us
    • Submit Article
    • Submit Source Code or Program
    • Share Your Interview Experience
  • Tools
    • IDE
    • CV Builder
    • Other Tools …
  • Jobs

MongoDB CRUD Operations Example and Syntax | Overview

Aniruddha Chaudhari/477/0
DBMS

MongoDB is one of the widely used NoSQL databases. You can read SQL vs NoSQL databases where I have explained the difference between SQL and NoSQL databases.

In this tutorial, I will be explaining CRUD (aka create, read, update and delete) operations on MongoDB. I will keep this short and simple.

Let’s begin with an overview of each MongoDB method.

Table of Contents

  • 1. Create Operations
    • i) insertOne()
    • ii) insertMany()
  • 2. Read Operations
    • i) findOne()
    • ii) find()
  • 3. Update Operations
    • i) updateOne()
    • ii) updateMany()
    • iii) update()
    • iv) replaceOne()
  • 4. Delete Operations
    • i) deleteOne()
    • ii) deleteMany()

1. Create Operations

To create one or more documents, there are two popular methods used.

i) insertOne()

Use this method to insert a single document in the collection.

Syntax:

db.collection.insertOne(data, options)

ii) insertMany()

Use this method to insert more than one document in the collection at a time.

Syntax:

db.collection.insertMany(data, options)

2. Read Operations

To read the document from the MongoDB collections here are some of the methods you can use.

i) findOne()

Use this method to retrieve the first matching document for the mentioned filtering criteria.

Syntax:

db.collection.findOne(filter, options)

Note: The concept of the cursor can not be applied here.

ii) find()

Use this method to find and retrieve multiple (or all) matching documents at once based on the filtering criteria.

Syntax:

 db.collection.find(filter, options)

This method actually returns a cursor object along with some metadata.

3. Update Operations

To update the document in the MongoDB collection, you can use the following methods.

i) updateOne()

It is used to update a single document in the collection that satisfies the given criteria.

Syntax:

db.collection.updateOne(criteria, data, options)

ii) updateMany()

It is used to update multiple documents in the collection that satisfy the given criteria.

Syntax:

db.collection.updateMany(criteria, data, options)

Note: If you specify empty braces {} as criteria, all documents will be updated with given data. Be careful if you while executing this command.

iii) update()

By default, it updates a single document in the collection. If you wish to update the multiple documents you need to specify {multi: true}.

As we have updateOne() and updateMany() to update one and multiple documents in the collections, respectively. You can avoid this method.

iv) replaceOne()

It is used to replace a single document in the collection that satisfies the given criteria.

Syntax:

db.collection.replaceOne(criteria, newData, options)

4. Delete Operations

Use the below methods to delete the documents from the MongoDB database collection.

i) deleteOne()

Use this method to delete the first document satisfying the given criteria.

Syntax:

db.collection.deleteOne(criteria, options)

ii) deleteMany()

Use this method to delete more than one document satisfying the given filtering criteria.

Syntax:

db.collection.deleteMany(criteria, options)

Note: If you specify empty braces {} as criteria, all documents will be deleted.

MongoDB is getting more popular now a day, just as MySQL. Both are used for a different purpose. Read MongoDB vs MySQL.

Besides the above-mentioned methods, there are couple more methods provided by MongoDB such as findOneAndReplace(), findOneAndUpdate() etc. The above-mentioned are the basic methods everyone should be aware of.

Any doubt or point to discuss? Let’s write in the comment section below.

MongoDB
Aniruddha Chaudhari
I am complete Python Nut, love Linux and vim as an editor. I hold a Master of Computer Science from NIT Trichy. I dabble in C/C++, Java too. I keep sharing my coding knowledge and my own experience on CSEstack.org portal.

Your name can also be listed here. Got a tip? Submit it here to become an CSEstack author.

Leave a Reply Cancel reply

Database Management System

  • DBMS- Introduction
  • DBMS- Advantages over File System
  • DBMS- Types of Database Keys
  • DBMS- ACID Properties 
  • DBMS- Read-Write Conflicts
  • DBMS- CHAR vs VARCHAR
  • DBMS- SQL vs NoSQL
  • DBMS- MySQL vs MongoDB
  • DBMS- MongoDB CRUD Operations
  • Why BigData? Growth Fact

SQL Programming

  • SQL- Complete Tutorial
  • SQL- Basic Concepts
  • SQL- SELECT Commands
  • SQL- JOIN Clause
  • SQL- Interview Questions

Programming Tutorials

C/C++ Programming

Python Programming

Java Programming

© 2022 – CSEstack.org. All Rights Reserved.

  • Home
  • Subscribe
  • Contribute Us
    • Share Your Interview Experience
  • Contact Us
  • About
    • About CSEstack
    • Campus Ambassador
  • Forum & Discus
  • Tools for Geek
  • LeaderBoard