Update Document in MongoDB

The method update () or save () in MongoDB is used to update the Document into a Collection.

The update () method updates the values ​​in the existing Document while the save () method replaces the existing Document with the transmitted Document in that save () method.

Method of updating () in MongoDB

The update () method updates the values ​​in the existing Document.

Syntax

The basic syntax of update () method is as follows:

 > db . COLLECTION_NAME . update ( SELECTIOIN_CRITERIA , UPDATED_DATA ) 

For example

You monitor the Collection named mycol with the following data:

 { "_id" : ObjectId ( 5983548781331adf45ec5 ), "title" : "MongoDB Overview" } { "_id" : ObjectId ( 5983548781331adf45ec6 ), "title" : "NoSQL Overview" } { "_id" : ObjectId ( 5983548781331adf45ec7 ), "title" : "Tutorials Point Overview" } 

The following example will set the new 'New MongoDB Tutorial' header of the Document with the title 'MongoDB Overview':

 > db . mycol . update ({ 'title' : 'MongoDB Overview' },{ $set :{ 'title' : 'New MongoDB Tutorial' }}) > db . mycol . find () { "_id" : ObjectId ( 5983548781331adf45ec5 ), "title" : "New MongoDB Tutorial" } { "_id" : ObjectId ( 5983548781331adf45ec6 ), "title" : "NoSQL Overview" } { "_id" : ObjectId ( 5983548781331adf45ec7 ), "title" : "Tutorials Point Overview" } > 

By default, MongoDB will only update a single Document, to update multiple Documents, you set the 'multi' parameter to true.

 > db . mycol . update ({ 'title' : 'MongoDB Overview' },{ $set :{ 'title' : 'New MongoDB Tutorial' }},{ multi : true }) 

Save () method in MongoDB

The save () method replaces the existing Document with the new Document passed in this save () method.

Syntax

The basic syntax of the save () method is as follows:

 > db . COLLECTION_NAME . save ({ _id : ObjectId (), NEW_DATA }) 

For example

The following example will replace the Document with _id as '5983548781331adf45ec7'.

 > db . mycol . save ( { "_id" : ObjectId ( 5983548781331adf45ec7 ), "title" : "Tutorials Point New Topic" , "by" : "Tutorials Point" } ) > db . mycol . find () { "_id" : ObjectId ( 5983548781331adf45ec5 ), "title" : "Tutorials Point New Topic" , "by" : "Tutorials Point" } { "_id" : ObjectId ( 5983548781331adf45ec6 ), "title" : "NoSQL Overview" } { "_id" : ObjectId ( 5983548781331adf45ec7 ), "title" : "Tutorials Point Overview" } > 

According to Tutorialspoint

Previous article: Query Document in MongoDB

Next lesson: Delete Document in MongoDB

4 ★ | 1 Vote

May be interested

  • Reference Database in MongoDBReference Database in MongoDB
    as shown in the relationship chapter in mongodb, to deploy a standardized database structure in mongodb, we use the referenced relationship concept, also known as manual references, in which we manipulate to store the id of the documents referenced in another document.
  • Limit records in MongoDBLimit records in MongoDB
    to limit the records in mongodb, you need to use the limit () method. the limit () method takes a parameter in a numeric format, which is the document number you want to display.
  • Advantages of MongoDBAdvantages of MongoDB
    any relation database has a unique schema design to index the data tables and relationships between those tables. meanwhile in mongodb there is no concept of relationship.
  • Data type in MongoDBData type in MongoDB
    mongodb supports many different data types. the following are some typical data types.
  • MongoDB malicious code attacks more than 26,000 victims in a weekMongoDB malicious code attacks more than 26,000 victims in a week
    malware that attacks the mongodb database has rekindled last week and after the weekend with the arrival of three new groups hijack more than 26,000 servers, of which one group attacked 22,000 machines.
  • Covered Query in MongoDBCovered Query in MongoDB
    when all fields in the query are part of the index, mongodb connects query conditions and returns the result by using the same index without looking inside the document. when indexes are present in ram, retrieving data from indexes is faster when compared to retrieving data by scanning all documents.
  • Learn about security features and authentication in MongoDBLearn about security features and authentication in MongoDB
    in the following article, we will continue to introduce security and authentication features in the mongodb database management system series. in essence, the basic account validation feature is available in mongodb, but has been disabled in default mode, and in this mode the system requires at least 1 active database ...
  • Insert Document in MongoDBInsert Document in MongoDB
    to insert data into collection in mongodb, you need to use the insert () or save () method.
  • Text Search in MongoDBText Search in MongoDB
    starting with version 2.4, mongodb started supporting text indexes to search within the string content.
  • Shard in MongoDBShard in MongoDB
    sharding is a process of storing data records across multiple devices and it is a method of mongodb to meet the requirement for increasing data. when the size of the data increases, a single device cannot be enough to store data.