The $rename operator in MongoDB lets you change field names without altering the stored values. Itโs useful for schema cleanup and consistent naming across collections, even for nested fields.
Renames fields without affecting data.
Supports nested fields via dot notation.
Allows multiple renames in one update.
Features of $rename Operator
Here are some features of $rename operator:
Schema Refactoring: Helps standardize and reorganize field names.
Nested & Arrays: Works with embedded documents using dot notation, but does not directly rename fields inside array elements.
Safe Behavior: No operation if the source field is missing, replaces the target if it exists
If a field does not exist in the document, the $rename operation will not perform any action. However, we can add a field first and then rename it. Hereโs how you can rename a middle name to middleName: