次の方法で共有


$eq

$eq演算子は、フィールドの値が指定した値と等しいドキュメントを照合するために使用されます。 $eq演算子は、クエリ述語の完全一致に基づいてドキュメントをフィルター処理し、特定の値、オブジェクト、配列を持つドキュメントを取得します。

構文

{
    field: {
        $eq: <value>
    }
}

パラメーター

パラメーター Description
field 比較するフィールド
value 比較対象の値

例示

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: ルート レベル フィールド$eqフィルターを使用する

「ボルダーイノベーション |[Home Security Place - Ankundingburgh]\(ホーム セキュリティ の場所 - Ankundingburgh\)"。名前フィールドに一致する$eq述語を使用してクエリを実行し、結果の ID フィールドと名前フィールドのみを射射します。

db.stores.find({
    name: {
        $eq: "Boulder Innovations | Home Security Place - Ankundingburgh"
    }
}, {
    name: 1
})

このクエリは、次の結果を返します。

[
    {
        "_id": "bda56164-954d-4f47-a230-ecf64b317b43",
        "name": "Boulder Innovations | Home Security Place - Ankundingburgh"
    }
]

例 2: 入れ子になったフィールド$eqフィルターを使用する

売上合計が 37,015 ドルの店舗を検索するには、入れ子になったフィールド sales.totalSales フィールドでドット表記を使用して、$eq演算子を使用してクエリを実行します。

db.stores.find({
    "sales.totalSales": {
        $eq: 37015
    }
}, {
    name: 1,
    "sales.totalSales": 1
})

これにより、次の結果が返されます。

[
    {
        "_id": "bda56164-954d-4f47-a230-ecf64b317b43",
        "name": "Boulder Innovations | Home Security Place - Ankundingburgh",
        "sales": { "totalSales": 37015 }
    }
]

例 3: 配列内の個々の項目に$eqを使用する

次のクエリでは、入れ子になった promotionEvents.discounts 配列内の個々の項目に対して等値述語を使用してドキュメントを取得します。

このクエリは、入れ子になった割引配列内のオブジェクトのいずれかで等値一致を検索します。

db.stores.find({
    "promotionEvents.discounts": {
        $eq: {
            categoryName: "Alarm Systems",
            discountPercentage: 5
        }
    }
}, {
    name: 1
}, {
    limit: 2
})

このクエリは、次の結果を返します。

[
  {
    "_id": "ece5bf6c-3255-477e-bf2c-d577c82d6995",
    "name": "Proseware, Inc. | Home Security Boutique - Schambergertown"
  },
  {
    "_id": "7baa8fd8-113a-4b10-a7b9-2c116e976491",
    "name": "Tailwind Traders | Home Security Pantry - Port Casper"
  }
]

例 4: $eqを使用して配列全体を照合する

このクエリは、promotionEvents.discounts 配列内のすべての値に対する完全一致に基づいてドキュメントを検索します。

db.stores.find({
    "promotionEvents.discounts": {
        $eq: [{
            categoryName: "Alarm Systems",
            discountPercentage: 5
        }, {
            categoryName: "Door Locks",
            discountPercentage: 12
        }]
    }
}, {
    name: 1
})

このクエリは、次の結果を返します。

[
    {
        "_id": "aa9ad64c-29da-42f8-a1f0-30e03bf04a2d",
        "name": "Boulder Innovations | Home Security Market - East Sheridanborough"
    }
]

配列全体の等価一致の場合、等値述語内の指定された値の順序も完全一致である必要があります。