$이름바꾸기

$rename 연산자는 업데이트 작업 중에 문서의 필드 이름을 바꾸는 데 사용됩니다. 이전 이름의 필드를 제거하고 지정된 이름의 새 필드를 만들어 원래 값을 유지합니다. 이 연산자는 문서 스키마를 재구성하거나 필드 명명 규칙을 수정하는 데 유용합니다.

문법

{
  $rename: {
    <field1>: <newName1>,
    <field2>: <newName2>,
    ...
  }
}

매개 변수

매개 변수 Description
field 이름을 바꿀 필드의 현재 이름입니다.
newName 필드의 새 이름입니다.

예시

스토어 컬렉션에서 이 샘플 문서를 고려합니다.

{
    "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
    "name": "First Up Consultants | Beverage Shop - Satterfieldmouth",
    "location": {
        "lat": -89.2384,
        "lon": -46.4012
    },
    "staff": {
        "totalStaff": {
            "fullTime": 8,
            "partTime": 20
        }
    },
    "sales": {
        "totalSales": 75670,
        "salesByCategory": [
            {
                "categoryName": "Wine Accessories",
                "totalSales": 34440
            },
            {
                "categoryName": "Bitters",
                "totalSales": 39496
            },
            {
                "categoryName": "Rum",
                "totalSales": 1734
            }
        ]
    },
    "promotionEvents": [
        {
            "eventName": "Unbeatable Bargain Bash",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 6,
                    "Day": 23
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 7,
                    "Day": 2
                }
            },
            "discounts": [
                {
                    "categoryName": "Whiskey",
                    "discountPercentage": 7
                },
                {
                    "categoryName": "Bitters",
                    "discountPercentage": 15
                },
                {
                    "categoryName": "Brandy",
                    "discountPercentage": 8
                },
                {
                    "categoryName": "Sports Drinks",
                    "discountPercentage": 22
                },
                {
                    "categoryName": "Vodka",
                    "discountPercentage": 19
                }
            ]
        },
        {
            "eventName": "Steal of a Deal Days",
            "promotionalDates": {
                "startDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 21
                },
                "endDate": {
                    "Year": 2024,
                    "Month": 9,
                    "Day": 29
                }
            },
            "discounts": [
                {
                    "categoryName": "Organic Wine",
                    "discountPercentage": 19
                },
                {
                    "categoryName": "White Wine",
                    "discountPercentage": 20
                },
                {
                    "categoryName": "Sparkling Wine",
                    "discountPercentage": 19
                },
                {
                    "categoryName": "Whiskey",
                    "discountPercentage": 17
                },
                {
                    "categoryName": "Vodka",
                    "discountPercentage": 23
                }
            ]
        }
    ]
}

예제 1: 최상위 필드 이름 바꾸기

필드 이름을 name 바꾸려면 두 필드 storeNamelocationstoreLocation모두에 $rename 연산자를 사용합니다.

db.stores.updateOne(
  { "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f" },
  {
    $rename: {
      "name": "storeName",
      "location": "storeLocation"
    }
  }
)

예제 2: 중첩 필드 이름 바꾸기

점 표기법을 사용하여 중첩 필드의 이름을 바꿀 수도 있습니다.

db.stores.updateOne(
  { "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f" },
  {
    $rename: {
      "location.lat": "location.latitude",
      "location.lon": "location.longitude",
      "staff.totalStaff.fullTime": "staff.totalStaff.fullTimeEmployees"
    }
  }
)

예제 3: 대량 이름 바꾸기 작업

를 사용하여 updateMany()여러 문서에서 필드 이름을 바꿀 수 있습니다.

db.stores.updateMany(
  {},
  {
    $rename: {
      "sales.totalSales": "sales.revenue",
      "staff.totalStaff": "staff.employeeCount"
    }
  }
)

중요합니다

지정된 $rename 필드가 없으면 해당 필드에는 작업이 적용되지 않습니다.

새 필드 이름이 이미 있는 $rename 경우 작업은 기존 필드를 덮어씁니다.

이 연산자는 $rename 배열 요소 내의 배열 요소 또는 필드 이름을 바꾸는 데 사용할 수 없습니다.

필드 이름은 빈 문자열이거나 null 문자를 포함할 수 없습니다.