TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

MongoDB Update Documents

MongoDB Update Documents for beginners and professionals with examples on CRUD, insert document, query document, update document, delete document, use database, projection etc.

<< Back to MONGODB

MongoDB update documents

In MongoDB, update() method is used to update or modify the existing documents of a collection.

Syntax:

db.COLLECTION_NAME.update(SELECTIOIN_CRITERIA, UPDATED_DATA)

Example

Consider an example which has a collection name TheDeveloperBlog. Insert the following documents in collection:

db.TheDeveloperBlog.insert(
   {
     course: "java",
     details: {
        duration: "6 months",
        Trainer: "Sonoo jaiswal"
     },
     Batch: [ { size: "Small", qty: 15 }, { size: "Medium", qty: 25 } ],
     category: "Programming language"
   }
)

After successful insertion, check the documents by following query:

>db.TheDeveloperBlog.find()

Output:

{ "_id" : ObjectId("56482d3e27e53d2dbc93cef8"), "course" : "java", "details" : 
{ "duration" : "6 months", "Trainer" : "Sonoo jaiswal" }, "Batch" : 
[ {"size" : "Small", "qty" : 15 }, { "size" : "Medium", "qty" : 25 } ],
 "category" : "Programming language" }

Update the existing course "java" into "android":

>db.TheDeveloperBlog.update({'course':'java'},{$set:{'course':'android'}})

Check the updated document in the collection:

>db.TheDeveloperBlog.find()

Output:

{ "_id" : ObjectId("56482d3e27e53d2dbc93cef8"), "course" : "android", "details" : 
{ "duration" : "6 months", "Trainer" : "Sonoo jaiswal" }, "Batch" : 
[ {"size" : "Small", "qty" : 15 }, { "size" : "Medium", "qty" : 25 } ],
 "category" : "Programming language" }




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf