$rename (Rename Field)

Last updated: 2026-10-09

$rename (Rename Field)

$rename renames a field. `{ $rename: { 'oldName': 'newName' } }`, with dotted paths for nested fields. If the new name already exists, the old value overwrites it; otherwise the field is created. $rename does not rename fields inside array elements, and moving a nested field does not create intermediate documents.

CategoryUpdate Field
MongoDB Version1.0+
Example Languagemongosh (Shell)

📝 Syntax

{ $rename: { <old field name>: <new field name> } }

⚙️ Parameters

old field nameThe source field name.
new field nameThe target field name; dotted paths allowed.

Returns:The update result

▶ Example

Run the commands below in your local mongosh (requires a running mongod):

// $rename: rename username to handle
// Ignored if the field is missing
db.users.updateOne(
  { _id: 1 },
  { $rename: { username: 'handle' } }
)

// Nested field rename
db.users.updateOne(
  { _id: 1 },
  { $rename: { 'profile.city': 'profile.cityName' } }
)

Output:

// username becomes handle; profile.city becomes profile.cityName

📌 Notes

📚 Related Tutorials

❓ FAQ

QCan $rename rename nested fields?
AYes, via dotted paths like 'profile.city'.
QWhat happens if the new field name already exists?
AThe old value overwrites the existing new field.
QDoes $rename handle fields inside arrays?
ANo, fields inside array elements need per-element updates.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏