MongoDB $rename 运算符

MongoDB $rename 运算符

MongoDB 中的 $rename 运算符是什么

MongoDB 提供了各种字段更新运算符来更新字段的值。$rename 运算符就是其中之一。$rename 运算符用于更改字段的名称。字段的新名称必须与字段的旧名称不同。

重要提示:

  1. 在对新名称执行 $set 操作之前,$rename 运算符会对旧名称和新名称都执行 $unset 操作。
  2. 它还可以与数组或嵌套文档一起使用。
  3. 根据需求,您可以在 findAndModify()、like()、update() 等函数中使用此运算符。
  4. 如果文档中没有给定的字段需要重命名,则 $rename 运算符不会做任何操作。

$rename 运算符的语法

{ $rename : {  : ,  : , ... } }

示例

在下面的示例中,我们正在处理:

Database: JavaTpoint
Collection: employees
Document: Six documents that contain the details of the employees
>db.employees.find().pretty()
        {
                "_id" : 1,
                "employee_name" : "Tin",
                "father_name" : "Thor",
                "department" : "Tester",
                "address" : "London",
                "joinning" : 2020,
                "phone_no" : 9856321478,
                "gender" : "Male",
                "age" : 20,
                "salary" : 10000
        }
        {
                "_id" : 2,
                "employee_name" : "John",
                "father_name" : "Mick",
                "department" : "Tester",
                "address" : "NewYork",
                "joining" : 2015,
                "phone_no" : 7896541478,
                "gender" : "Male",
                "age" : 23,
                "salary" : 20000,
                "report_lastDate" : ISODate("2021-08-05T00:00:00Z") 
        }
        {
                "_id" : 3,
                "employee_name" : "Ammy john",
                "father_name" : "John",
                "department" : "Software developer",
                "address" : "London",
                "joining" : 2019,
                "phone_no" : 7985631478,
                "gender" : "Female",
                "age" : 26,
                "salary" : 15000
        }
        {
                "_id" : 4,
                "employee_name" : "Reeza",
                "father_name" : "Reeza Hendricks",
                "department" : "Tester",
                "address" : "USA",
                "joining" : 2020,
                "phone_no" : 7412563278,
                "gender" : "Male",
                "age" : 22,
                "salary" : 20000
        }
        { 
                "_id" : 5,
                "employee_name" : "John Lewis",
                "father_name" : "Lewis",
                "department" : "Software developer",
                "address" : "London",
                "joining" : 2015,
                "phone_no" : 9632587418,
                "gender" : "Male",
                "age" : 25,
                "salary" : 25000,
        }
        {
                "_id" : 6,
                "employee_name" : "Temba",
                "father_name" : "George",
                "department" : "Tester",
                "address" : "NewYork",
                "joining" : 2018,
                "phone_no" : 8965247418,
                "gender" : "Male",
                "age" : 24,
                "salary" : {
                             "first_month" : 15000,
                             "second_month" : 18000,
                             "bonus" : 2000
                           }
        }

示例1:重命名单个字段

在这个示例中,我们将测试部门员工文档中的”joining”字段名称更改为”joinYear”。

db.employees.update( {"department": "Tester"},
                {$rename: {"joining": "joinYear"}})

输出:

{
     "_id" : 1,
     "employee_name" : "Tin",
     "father_name" : "Thor",
     "department" : "Tester",
     "address" : "London",
     "joinYear" : 2020,
     "phone_no" : 9856321478,
     "gender" : "Male",
     "age" : 20,
     "salary" : 10000
}
{
     "_id" : 2,
     "employee_name" : "John",
     "father_name" : "Mick",
     "department" : "Tester",
     "address" : "NewYork",
     "joinYear" : 2015,
     "phone_no" : 7896541478,
     "gender" : "Male",
     "age" : 23,
     "salary" : 20000,
     "report_lastDate" : ISODate("2021-08-05T00:00:00Z") 
}
{
     "_id" : 4,
     "employee_name" : "Reeza",
     "father_name" : "Reeza Hendricks",
     "department" : "Tester",
     "address" : "USA",
     "joinYear" : 2020,
     "phone_no" : 7412563278,
     "gender" : "Male",
     "age" : 22,
     "salary" : 20000
}
{
     "_id" : 6,
     "employee_name" : "Temba",
     "father_name" : "George",
     "department" : "Tester",
     "address" : "NewYork",
     "joinYear" : 2018,
     "phone_no" : 8965247418,
     "gender" : "Male",
     "age" : 24,
     "salary" : {
                 "first_month" : 15000,
                 "second_month" : 18000,
                 "bonus" : 2000
                }
}

示例2:将文档中多个字段的名称进行更名

在这个示例中,我们将”phone_no”字段的名称更名为”contact_no”,并应用于employees集合中的所有文档。

db.employees.updateMany( {}, {$rename: {"phone_no": "contact_no"}})

输出:

{
     "_id" : 1,
     "employee_name" : "Tin",
     "father_name" : "Thor",
     "department" : "Tester",
     "address" : "London",
     "joining" : 2020,
     "contact_no" : 9856321478,
     "gender" : "Male",
     "age" : 20,
     "salary" : 10000
}
{
     "_id" : 2,
     "employee_name" : "John",
     "father_name" : "Mick",
     "department" : "Tester",
     "address" : "NewYork",
     "joining" : 2015,
     "contact_no" : 7896541478,
     "gender" : "Male",
     "age" : 23,
     "salary" : 20000,
     "report_lastDate" : ISODate("2021-08-05T00:00:00Z") 
}
{
     "_id" : 3,
     "employee_name" : "Ammy john",
     "father_name" : "John",
     "department" : "Software developer",
     "address" : "London",
     "joining" : 2019,
     "contact_no" : 7985631478,
     "gender" : "Female",
     "age" : 26,
     "salary" : 15000
}
{
     "_id" : 4,
     "employee_name" : "Reeza",
     "father_name" : "Reeza Hendricks",
     "department" : "Tester",
     "address" : "USA",
     "joining" : 2020,
     "contact_no" : 7412563278,
     "gender" : "Male",
     "age" : 22,
     "salary" : 20000
}
{ 
     "_id" : 5,
     "employee_name" : "John Lewis",
     "father_name" : "Lewis",
     "department" : "Software developer",
     "address" : "London",
     "joining" : 2015,
     "contact_no" : 9632587418,
     "gender" : "Male",
     "age" : 25,
     "salary" : 25000,
}
{
     "_id" : 6,
     "employee_name" : "Temba",
     "father_name" : "George",
     "department" : "Tester",
     "address" : "NewYork",
     "joining" : 2018,
     "contact_no" : 8965247418,
     "gender" : "Male",
     "age" : 24,
     "salary" : {
                 "first_month" : 15000,
                 "second_month" : 18000,
                 "bonus" : 2000
                }
}

示例3:重命名嵌套文档中的字段

在此示例中,我们将名为“salary.first_month”的字段名称在员工文档中更改为“salary.month”,该文档的employee_name是Temba。

db.employee.update({"employee_name": "Temba"},
                {$rename: {"salary.first_month": "salary.month"}})

输出:

{
     "_id" : 6,
     "employee_name" : "Temba",
     "father_name" : "George",
     "department" : "Tester",
     "address" : "NewYork",
     "joining" : 2018,
     "phone_no" : 8965247418,
     "gender" : "Male",
     "age" : 24,
     "salary" : {
                 "month" : 15000,
                 "second_month" : 18000,
                 "bonus" : 2000
                }
}

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程