Renaming a field using $rename operator
In this example, we are renaming the name of department field to unit in the employee’s document whose first name is Om.
Python3
db.Employee.update({"name.first": "Om"}, {$rename: {"department": "unit"}}) |
MongoDB – Field Update Operators
MongoDB provides different types of field update operators to update the values of the fields of the documents that matches the specified condition. The following table contains the field update operators:
Operator | Description |
---|---|
$currentDate | This operator is used to set the value of a field to current date, either as a Date or a Timestamp. |
$inc | This operator is used to increment the value of the field by the specified amount. |
$min | This operator is used only to update the field if the specified value is less than the existing field value |
$max | This operator is used only to update the field if the specified value is greater than the existing field value. |
$mul | This operator is used to multiply the value of the field by the specified amount. |
$rename | This operator is used to rename a field. |
$setOnInsert | This operator is used to set the value of a field if an update results in an insert of a document. It has no effect on update operations that modify existing documents. |
In the following examples, we are working with:
Database: w3wiki
Collection: Employee
Document: two documents that contain the details of the employees in the form of field-value pairs.