MongoDB Save Document-Programmershelper

0
368
Spread the love

Introduction

This article will provide step by step basic concept of MongoDB. where you can easily understand the core concept of MongoDB.The following method <db.collection.save> is used to save a document. the save method () works in two different ways inserts a new document or updates an existing document depending on specific criteria.

Prerequisite

It would be better if you have the basic knowledge of RDBMS concept. Actually, you are going to learn a high-performance database.It will good to cover my below articles before this.Its help to learn more about MongoDB.In which I explained.Click here MongoDB find document step by step guideline and MongoDB update document step by step guideline.

 Save Method()

  • Having not_id

if the document <having not a specific _id attribute> then save_method works insert specific value in the document.

  • Having _id

if the document <having specific _id attribute> then save_method works update or replace existing value in the document.

Implementation Section

Syntax

The following is syntax of MongoDB save document

Replace the existing document with the new document using save method().

Example
db.students.save({_id:ObjectId(‘string_id’), “name”: “Zzz”, “age”: 23, “gender”: “male”}

Practical work

if the document having not specific _id attribute then save_method working as an insert.show in below fig.

Output:

The following example will add a new field <rank:1> using save method () and insert a field in a document. show pic

Alt tag MongoDB save document

In MongoDB save the document. if the document having specific _id attribute then save_method working like the update or replace a field.

Output:

The following example will set the new name <NewMongoDB tutorial > of the documents whose name<nprogrammershelpes>show in below fig.

Alt tag MongoDB save document

Conclusion

I wish I could tell you that a great site of MongoDB.you just understands key element above post-MongoDB saves the document.for more learn about click here. Save the document in MongoDB.I hope you will understand this lecture.Thank you for reading this lecture. Hope you got the idea.