Updating Documents (updateOne,updateMnay,$set) (Akshata Gargote)



                        INTRODUCTION

 

Updating documents in MongoDB allows you to modify existing records in a collection without replacing the entire document. MongoDB provides methods like updateOne() to update a single matching document, updateMany() to update multiple documents at once, and the $set operator to change specific fields while keeping the rest of the document intact. These update operations make it easy to maintain and modify data dynamically in real-time applications.

        

                              EXPLANATION

In MongoDB, updating documents is done using methods like updateOne(), updateMany(), and the $set operator. The updateOne() method updates the first document that matches a given filter, while updateMany() updates all matching documents at once. The $set operator is used to of the data. These operations help efficiently manage and modify data within collection.

 

Syntax

1. updateOne()

javascript

CopyEdit

db.collection.updateOne(

   <filter>,

   { $set: { <field1>: <value1>, <field2>: <value2>, ... } },

   { upsert: <boolean> }

)

 2. updateMany()

javascript

CopyEdit

db.collection.updateMany(

   <filter>,

   { $set: { <field1>: <value1>, <field2>: <value2>, ... } },

   { upsert: <boolean> }

)

3. $set Operator

javascript

CopyEdit

{ $set: { <field>: <value> } }

filter → The condition to match documents.
$set → Updates only specified fields without replacing the whole document.
upsert (optional) → Inserts a new document if no match

 

Screenshots:

Name: Akshata Gargote

College: Sri Balaji University,Pune

School: School Of ComputerStudies

Class: TY-BCA(D)

 

 

Top of Form

 

Bottom of Form

 

Comments

Post a Comment

Popular posts from this blog

BSON VS JSON --- What's The Difference

Introduction to MongoDB Compass GUI

VEDANT PATIL - BCA2302100