- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to use save() correctly in MongoDB?
Use the db.collection.save() to update an existing document or inserts a new document, depending on its document parameter. Let us create a collection with documents −
> db.demo481.save({"FirstName":"Chris","LastName":"Brown"}); WriteResult({ "nInserted" : 1 }) > db.demo481.save({"FirstName":"David","LastName":"Miller"}); WriteResult({ "nInserted" : 1 }) > db.demo481.save({"FirstName":"John","LastName":"Doe"}); WriteResult({ "nInserted" : 1 }) > db.demo481.save({"FirstName":"John","LastName":"Smith"}); WriteResult({ "nInserted" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo481.find();
This will produce the following output −
{ "_id" : ObjectId("5e82db39b0f3fa88e227909c"), "FirstName" : "Chris", "LastName" : "Brown" } { "_id" : ObjectId("5e82db45b0f3fa88e227909d"), "FirstName" : "David", "LastName" : "Miller" } { "_id" : ObjectId("5e82db4db0f3fa88e227909e"), "FirstName" : "John", "LastName" : "Doe" } { "_id" : ObjectId("5e82db52b0f3fa88e227909f"), "FirstName" : "John", "LastName" : "Smith" }
Advertisements