Update Many Query in Mongo

Update Many mongo documents using $set operator under Employee_Detail collection.

Example:

[{ 
    "_id" : ObjectId("60179dd62381fc69d5f1c599"), 
    "Emp_Name" : "Andrew", 
    "Emp_Age" : NumberInt(29), 
    "Emp_Salary" : NumberInt(24000), 
    "Emp_Joining_Date" : ISODate("2020-07-03T18:30:00.000+0000")
}
{ 
    "_id" : ObjectId("60179dd62381fc69d5f1c59a"), 
    "Emp_Name" : "Sam", 
    "Emp_Age" : NumberInt(34), 
    "Emp_Salary" : NumberInt(22000), 
    "Emp_Joining_Date" : ISODate("2020-07-03T18:30:00.000+0000")
}
{ 
    "_id" : ObjectId("60179dd62381fc69d5f1c59b"), 
    "Emp_Name" : "Rahul", 
    "Emp_Age" : NumberInt(54), 
    "Emp_Salary" : NumberInt(34000), 
    "Emp_Joining_Date" : ISODate("2020-07-03T18:30:00.000+0000")
}
]


Approach

Query: To update many Document  Emp_salary  where Emp_salary is less than and equal to 25000

db.Employee_Detail.updateMany(
{"Emp_salary":{$lte:25000}},
{$set:{"Emp_salary":30000}}
);

Output: 

{ 
    "acknowledged" : true, 
    "matchedCount" : NumberInt(2), 
    "modifiedCount" : NumberInt(2)
}


No comments:

Post a Comment