0% found this document useful (0 votes)
18 views11 pages

CRUD With MongoDB

This document discusses MongoDB CRUD operations. It introduces MongoDB and the CRUD operations of create, read, update, and delete. It provides syntax examples and explanations for performing each operation, such as inserting single and multiple documents, finding documents, limiting results, and deleting. It also includes some interview questions about MongoDB updates.

Uploaded by

estudy044
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views11 pages

CRUD With MongoDB

This document discusses MongoDB CRUD operations. It introduces MongoDB and the CRUD operations of create, read, update, and delete. It provides syntax examples and explanations for performing each operation, such as inserting single and multiple documents, finding documents, limiting results, and deleting. It also includes some interview questions about MongoDB updates.

Uploaded by

estudy044
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Lesson:

CRUD with MongoDB


Topics
Introductio
Prerequisite
Create operatio
Read operatio
Update operatio
Delete operation

Introduction

MongoDB, a NoSQL database, supports the four fundamental operations: Create, Read, Update, and Delete,
commonly called CRUD. This article will guide you through the essential concepts and implementation of CRUD
operations using MongoDB.

We will begin by creating a dedicated database named "my-db" to serve as the focal point for all the examples
within this module. This database will act as the sandbox where we perform various CRUD operations, allowing
us to explore and understand the functionalities of MongoDB in a focused and organized manner throughout
this module.

Create the database using the “use” command in MongoDB shell

i.e

Note - The database created i.e. “my-db” will be visible after data or collections are added.

Full Stack Web Development


Pre-requisites

Before diving into CRUD operations with MongoDB, ensure you have the following prerequisites

MongoDB Installed: Install MongoDB on your machine. You can find installation instructions on the official
MongoDB website
Basic knowledge of MongoDB key concept
MongoDB Shell installed: The MongoDB Shell is a command-line interface to interact with MongoDB. It's a
powerful tool for executing queries and commands directly.
MongoDB Compass installed(Optional): MongoDB Compass is a graphical user interface for MongoDB.
Though optional, it can provide a visual representation of your data and aid in understanding the operations.

Create operation

In MongoDB, data is stored in documents. Learn how to perform the Create operation using the insertOne and
insertMany methods. Dive into examples that illustrate the creation of single and multiple documents,
understanding the nuances of document structure.

Full Stack Web Development


Syntax

Examples of create operation

Inserting single documents -

Inserting multiple documents -

Full Stack Web Development


Read operation

In MongoDB, data in documents can be fetched using the find method. Discover how to find documents in a
collection, apply filters, limit results, and sort data. Uncover the versatility of MongoDB queries to extract
precisely the information you need.
Syntax -

Example read operation


Find all documents in a collection

Full Stack Web Development


Find documents that match a specific condition, here find a user name with “john”

Limit the number of returned documents

Sorting documents, 1 for ascending & -1 for descending, sorting based on _id field

Retrieve a single document that matches the query

Full Stack Web Development


Update operation

Updating documents allows you to modify existing data, In MongoDB, the updateOne and updateMany
methods along with the $set operator are used to modify specific fields within documents in the collections and
database.
Syntax -

Example of Update operation


Updating single document

Full Stack Web Development


Updating multiple documents

Delete operation

Deleting documents removes data from a collection, In MongoDB the deleteOne and deleteMany methods are
used to delete documents within the documents inside the collection and database.

Full Stack Web Development


Syntax -

Example of Update operation


Deleting single documents

Deleting multiple documents

Full Stack Web Development


Interview points

Question 1 - What is the purpose of the $unset operator in MongoDB updates?

Answer - The $unset operator removes a specified field from a document. It is useful when you need to
eliminate a field or multiple fields from documents, effectively "unsetting" them.

Question 2 - How can you find documents in MongoDB where a specific field exists? Provide an example

Answer - You can use the $exists operator to find documents where a specific field exists:

This query retrieves documents where the field “name” or provided key name exists.

Full Stack Web Development


THANK
YOU !

You might also like