共用方式為


$eq

運算子 $eq 是用來比對欄位值等於指定值的檔。 $eq運算符會根據查詢述詞的完全符合來篩選檔,以擷取具有特定值、物件和數位的檔。

語法

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

參數

參數 Description
field 要比較的欄位
value 要與之比較的值

範例

請參考商店集合中的此範例檔。

{
    "_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篩選

尋找名稱為 「Boulder Innovations」 的商店 |Home Security Place - Ankundingburgh“,使用$eq述詞執行查詢,以符合名稱字段,並只投影結果中的標識符和名稱字段。

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

備註

對於整個陣列的相等比對,相等述詞中指定值的順序也必須完全相符。