次の方法で共有


削除する

delete コマンドは、コレクションからドキュメントを削除するために使用されます。 指定したクエリ フィルターに基づいて、1 つのドキュメントまたは複数のドキュメントを削除できます。

構文

delete コマンドの基本的な構文は次のとおりです。

db.collection.deleteOne(
   <filter>,
   <options>
)

db.collection.deleteMany(
   <filter>,
   <options>
)

パラメーター

パラメーター Description
<filter> 削除の条件を指定するドキュメント。 フィルターに一致するドキュメントのみが削除されます
options Optional. 削除操作のオプションを指定するドキュメント。 一般的なオプションには、writeConcern と照合順序が含まれます

StoreData データベースの stores コレクションのこのサンプル ドキュメントについて考えてみましょう。

{
    "_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 - コレクション内のすべてのドキュメントを削除する

db.stores.deleteMany({})

例 2 - 指定したクエリ フィルターに一致するドキュメントを削除する

db.stores.deleteOne({"_id": "68471088-4d45-4164-ae58-a9428d12f310"})

例 3 - 指定したクエリ フィルターに一致するすべてのドキュメントを削除する

db.stores.deleteMany({"promotionEvents.discounts.discountPercentage": 21}, {"limit": 0})

例 3 - 指定されたクエリ フィルターに一致する多数のドキュメントの 1 つだけを削除する

db.stores.deleteMany({"promotionEvents.discounts.discountPercentage": 21}, {"limit": 1})