다음을 통해 공유


업데이트

update 명령은 컬렉션 내에서 기존 문서를 수정하는 데 사용됩니다. 이 update 명령은 필터링 조건에 따라 하나 이상의 문서를 업데이트하는 데 사용할 수 있습니다. 필드 값을 변경하고 새 필드와 값을 추가할 수 있으며 기존 필드를 제거할 수 있습니다.

예제(들)

StoreData 데이터베이스의 Store 컬렉션에서 이 샘플 문서를 고려합니다.

{
    "_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 - $inc 연산자를 사용하여 단일 문서 업데이트

totalSales를 10씩 증가시키고 지정한 _id 있는 문서의 정규직 직원 수를 줄입니다.

db.stores.updateOne({"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4"}, {"$inc": {"sales.salesByCategory.0.totalSales": 10, "staff.totalStaff.fullTime": -6}})

예제 2 - $min 연산자를 사용하여 단일 문서 업데이트

필드의 현재 값이 10보다 큰 경우 문서의 totalStaff 수를 지정된 _id 10으로 업데이트합니다.

db.stores.updateOne({"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4"}, {"$min": {"staff.totalStaff.fullTime": 10}})

예제 3 - $max 연산자를 사용하여 단일 문서 업데이트

필드의 현재 값이 14보다 작은 경우 문서의 totalStaff 수를 지정된 _id 14로 업데이트합니다.

db.stores.updateOne({"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4"}, {"$max": {"staff.totalStaff.fullTime": 14}})

예제 4 - $mul 연산자를 사용하여 단일 문서 업데이트

지정한 _id 값을 가진 문서의 파트타임 직원 수를 2로 곱합니다.

db.stores.updateOne({"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4"}, {"$mul": {"staff.totalStaff.partTime": 2}})

예제 5 - $rename 연산자를 사용하여 단일 문서 업데이트

totalSales 및 totalStaff 필드의 이름을 각각 fullSales 및 staffCounts로 바꿉니다.

db.stores.updateOne({"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4"}, {"$rename": {"sales.totalSales": "sales.fullSales", "staff.totalStaff": "staff.staffCounts"}})

예제 6 - $set 연산자를 사용하여 단일 문서 업데이트

지정한 _id 값을 사용하여 문서의 fullSales 필드를 3700으로 설정합니다.

db.stores.updateOne({"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4"}, {"$set": {"sales.fullSales": 3700}})

예제 7 - $unset 연산자를 사용하여 단일 문서 업데이트

지정한 _id 값을 사용하여 문서의 위치 개체에서 lon 필드를 제거합니다.

db.stores.updateOne({"_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4"}, {"$unset": {"location.lon": ""}})

예제 8 - 여러 문서 업데이트

2월에 첫 번째 프로모션 이벤트가 시작되는 모든 문서를 업데이트하여 3월에 시작합니다.

db.stores.updateMany({"promotionEvents.0.promotionalDates.startDate.Month": 2}, {"$inc": {"promotionEvents.0.promotionalDates.startDate.Month": 1}})

예제 9 - 단일 문서 Upsert

쿼리 필터에 지정된 문서가 컬렉션에 없는 경우 새 문서를 만들려면 upsert 플래그를 true로 설정합니다.

db.stores.updateOne({"_id": "NonExistentDocId"}, {"$set": {"name": "Lakeshore Retail", "sales.totalSales": 0}}, {"upsert": true})