
- MongoDB Tutorial
- MongoDB - Home
- MongoDB - Overview
- MongoDB - Advantages
- MongoDB - Environment
- MongoDB - Data Modeling
- MongoDB - Create Database
- MongoDB - Drop Database
- MongoDB - Create Collection
- MongoDB - Drop Collection
- MongoDB - Data Types
- MongoDB - Insert Document
- MongoDB - Query Document
- MongoDB - Update Document
- MongoDB - Delete Document
- MongoDB - Projection
- MongoDB - Limiting Records
- MongoDB - Sorting Records
- MongoDB - Indexing
- MongoDB - Aggregation
- MongoDB - Replication
- MongoDB - Sharding
- MongoDB - Create Backup
- MongoDB - Deployment
- MongoDB - Java
- MongoDB - PHP
- Advanced MongoDB
- MongoDB - Relationships
- MongoDB - Database References
- MongoDB - Covered Queries
- MongoDB - Analyzing Queries
- MongoDB - Atomic Operations
- MongoDB - Advanced Indexing
- MongoDB - Indexing Limitations
- MongoDB - ObjectId
- MongoDB - Map Reduce
- MongoDB - Text Search
- MongoDB - Regular Expression
- Working with Rockmongo
- MongoDB - GridFS
- MongoDB - Capped Collections
- Auto-Increment Sequence
- MongoDB Useful Resources
- MongoDB - Questions and Answers
- MongoDB - Quick Guide
- MongoDB - Useful Resources
- MongoDB - Discussion
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" }
- Related Articles
- How to use % wildcard correctly in MySQL?
- How to correctly use WITH ROLLUP in MySQL?
- How to use formatting with printf() correctly in Java?
- How to correctly use DELIMITER in a MySQL stored procedure?
- How can I save new Date() in MongoDB?
- How to create a collection correctly in MongoDB to avoid “ReferenceError: Not defined” error?
- How to correctly use INSERT INTO … SELECT in MySQL to avoid Error 1064?
- How to use $regex in MongoDB?
- How to use $type in MongoDB?
- Set variable value in MongoDB save() method
- How to correctly use delimiter in a MySQL stored procedure and insert values?
- How to use collMod in MongoDB runCommand()?
- How to use deleteOne() function in MongoDB?
- How to use quotes correctly while using LIKE with search variable in MySQL in Java?
- How to use MongoDB Aggregate to sort?

Advertisements