Reputation: 1701
I have a document that stores employee's name and his salary details. The document stores salary details of each month. Note that the field 'Employee_Salary' is not an array. This is my document.
{
"_id" : 5.0000000000000000,
"Employee_Name" : "John Karrl",
"Employee_Salary" : {
"April-2015" : {
"actual_salary" : 300,
"bonus" : 0,
"penalty" : 0,
"bonus_pen_detail" : "NA",
"month_paid" : "April",
"year_paid" : 2015,
"total_amount_paid" : 300
}
}
}
Now I want to update this document to insert or add new salary details of the next month, which is May-2015. I have tried using '$push' command but it does not work and gives me the following error.
'The field 'Employee_Salary' must be an array but is of type Object in document {_id: 5.0}'.
Upvotes: 2
Views: 69
Reputation: 2868
$push
can only be applied to array elements. Since Employee_Salary
is an object you must perform a $set
operation as shown below:
db.employee.update(
{_id: 5.0},
{$set: {
'Employee_Salary.May-2015': {
"actual_salary" : 300,
"bonus" : 0,
"penalty" : 0,
"bonus_pen_detail" : "NA",
"month_paid" : "May",
"year_paid" : 2015,
"total_amount_paid" : 300
}
}}
)
Upvotes: 1