Reputation: 1701
I have a document that stores detail of employees. This is my document structure.
{
"emp_id" : 1,
"Employee_Name" : "Fareed Khan Jan",
"Employee_Contact" : "07492897789",
"Employee_Gender" : "Male",
"Employee_Address" : "Lewisham",
"IS_Available" : "YES",
"Employee_Reports" : [
{
"Report_Title" : "Initial Project Design",
"Report_Details" : "This is a sample report.",
"Date_Submit" : "26/10/2015"
}
],
"Employee_Salary" : [
{
"month_year" : "Jan-2015",
"actual_salary" : 200.0000000000000000,
"bonus" : 0.0000000000000000,
"penalty" : 0.0000000000000000,
"bonus_pen_detail" : "NA",
"total_amount_paid" : 200.0000000000000000
},
{
"month_year" : "Feb-2015",
"actual_salary" : 200.0000000000000000,
"bonus" : 0.0000000000000000,
"penalty" : 0.0000000000000000,
"bonus_pen_detail" : "NA",
"total_amount_paid" : 200.0000000000000000
}
]
}
Now I want to update the embedded document 'Employee_Salary' where "month-year" is 'Feb-2015'. I have written the following query but it deletes all the data inside 'Employee_Salary' and update the one. I don't want to delete other data inside this.
db.employees.update
(
{ 'emp_id': 1, 'Employee_Salary.month_year' : "Feb-2015" },
{ '$set': {
'Employee_Salary': [
{
"month_year" : "Feb-2015",
"actual_salary" : 200.0000000000000000,
"bonus" : 80.0000000000000000,
"penalty" : 0.0000000000000000,
"bonus_pen_detail" : "NA",
"total_amount_paid" : 280.0000000000000000
}
]
}}
)
Upvotes: 0
Views: 176
Reputation: 1664
it's because you tell it to $set
Employee_Salary with the new array.
To modify the one you looked for in your find, you have to use the $
operator:
{ '$set': {
"Employee_Salary.$.bonus" : "80.0000000000000000"
}}
Upvotes: 1