- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
Update salary field value with 10 percent of each employee in MongoDB
Let us first create a collection with documents −
> db.demo417.insertOne({"EmployeeName":"Chris","EmployeeSalary":500}); { "acknowledged" : true, "insertedId" : ObjectId("5e723ebbb912067e57771ae4") } > db.demo417.insertOne({"EmployeeName":"Mike","EmployeeSalary":1000}); { "acknowledged" : true, "insertedId" : ObjectId("5e723ed7b912067e57771ae5") }
Display all documents from a collection with the help of find() method −
> db.demo417.find();
This will produce the following output −
{ "_id" : ObjectId("5e723ebbb912067e57771ae4"), "EmployeeName" : "Chris", "EmployeeSalary" : 500 } { "_id" : ObjectId("5e723ed7b912067e57771ae5"), "EmployeeName" : "Mike", "EmployeeSalary" : 1000 }
Following is the query to update salary field value with 10 percent of each employee in employee collection −
> db.demo417.update({ "EmployeeName": { $in: ["Chris", "Mike"] } }, { $mul: { EmployeeSalary: 1.1 }},{multi:true}); WriteResult({ "nMatched" : 2, "nUpserted" : 0, "nModified" : 2 })
Display all documents from a collection with the help of find() method −
> db.demo417.find();
This will produce the following output −
{ "_id" : ObjectId("5e723ebbb912067e57771ae4"), "EmployeeName" : "Chris", "EmployeeSalary" : 550 } { "_id" : ObjectId("5e723ed7b912067e57771ae5"), "EmployeeName" : "Mike", "EmployeeSalary" : 1100 }
- Related Articles
- MySQL edit and update records including employee salary
- Update MongoDB field using value of another field?
- MongoDB query to update each field of documents in collection with a formula?
- Update _id field in MongoDB
- MongoDB query to update array with another field?
- Update MongoDB variable value with variable itself?
- Display only the employee names with specific salaries in MongoDB documents with employee records?
- How to update _id field in MongoDB?
- Update all the values of a field with a specific string in MongoDB?
- How to run MongoDB query to update only a specific field value?
- Update field in exact element array in MongoDB?
- Replace an array field value with MongoDB?
- Conditional update depending on field matched in MongoDB
- Want to update inner field in a MongoDB
- The salary for a month of an employee is Rs. 4000. The annual salary of the employee is1) RS 480002) RS 240003) RS 120004) RS 8000

Advertisements