運算子 $ne 會擷取欄位的值不等於指定值的檔。
語法
{
field: {
$ne: 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 - 尋找名稱不是 “Fourth Coffee” 的商店
若要尋找名稱不是 「Fourth Coffee」 的商店,請先在名稱欄位中使用$ne執行查詢。 然後只投影結果檔的名稱,並將結果限製為結果集的一個存放區。
db.stores.find({
name: {
$ne: "Fourth Coffee"
}
}, {
_id: 1,
name: 1
}, {
limit: 1
})
此查詢傳回的第一個結果是:
[
{
"_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f",
"name": "First Up Consultants | Bed and Bath Center - South Amir"
}
]
範例 2 - 尋找 2024 年沒有促銷事件的市集
若要尋找在 2024 年未啟動之促銷事件的存放區,請先在巢狀 startDate 字段上使用 $ne執行查詢。 然後投影存放區所提供的名稱和促銷,並將結果限製為結果集中的一份檔。
db.stores.find({
"promotionEvents.promotionalDates.startDate": {
$ne: "2024"
}
}, {
name: 1,
"promotionEvents.promotionalDates.startDate": 1
}, {
limit: 1
})
此查詢傳回的第一個結果是:
[
{
"_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f",
"name": "First Up Consultants | Bed and Bath Center - South Amir",
"promotionEvents": [
{
"promotionalDates": { "startDate": { "Year": 2024, "Month": 9, "Day": 21 } }
}
]
}
]