
- 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
MongoDB query to update only a single item from voting (up and down) records?
Let us first create a collection with documents −
> db.demo57.insertOne({"Votes":{"VoterName":"Chris","TotalVote":50}}); { "acknowledged" : true, "insertedId" : ObjectId("5e285bb8cfb11e5c34d8991a") } > db.demo57.insertOne({"Votes":{"VoterName":"David","TotalVote":101}}); { "acknowledged" : true, "insertedId" : ObjectId("5e285bc3cfb11e5c34d8991b") }
Display all documents from a collection with the help of find() method −
> db.demo57.find();
This will produce the following output −
{ "_id" : ObjectId("5e285bb8cfb11e5c34d8991a"), "Votes" : { "VoterName" : "Chris", "TotalVote" : 50 } } { "_id" : ObjectId("5e285bc3cfb11e5c34d8991b"), "Votes" : { "VoterName" : "David", "TotalVote" : 101 } }
Here is the query to update only a single item (TotalVote) −
> db.demo57.update({"Votes.VoterName":"David" },{ $inc : { "Votes.TotalVote" : 19 } }); WriteResult({ "nMatched" : 1, "nUpserted" : 0, "nModified" : 1 })
Display all documents from a collection with the help of find() method −
> db.demo57.find();
This will produce the following output −
{ "_id" : ObjectId("5e285bb8cfb11e5c34d8991a"), "Votes" : { "VoterName" : "Chris", "TotalVote" : 50 } } { "_id" : ObjectId("5e285bc3cfb11e5c34d8991b"), "Votes" : { "VoterName" : "David", "TotalVote" : 120 } }
- Related Articles
- MySQL update multiple records in a single query?
- Update a single list item of a MongoDB document?
- Update only a single document in MongoDB
- MongoDB query to update only certain fields?
- A single MySQL query to update only specific records in a range without updating the entire column
- Update multiple rows in a single MongoDB query?
- MongoDB query to update a MongoDB row with only the objectid
- MongoDB query to remove item from array?
- MongoDB query to search date records using only Month and Day
- Update only a single MongoDB document without deleting any date
- MongoDB Query to search for records only in a specific hour?
- MySQL query to update only a single field in place of NULL
- How to run MongoDB query to update only a specific field value?
- MySQL query to display only the records that contains single word?
- Selecting only a single field from MongoDB?

Advertisements