MongoDB $rename 运算符

MongoDB $rename 运算符 介绍

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

MongoDB $rename 运算符 语法

{ $rename : { <field 1> : <new name 1>, <field 2> : <new name 2>, ... } }  

要点:

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

MongoDB $rename 运算符 例子

在以下示例中,我们正在使用:

Database: Yiidian
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:重命名单个字段

在此示例中,我们将部门为 Tester 的员工文档中的“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”。

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:重命名嵌套文档中的字段

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

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
                }
}

 

热门文章

优秀文章