Anteckning
Åtkomst till den här sidan kräver auktorisering. Du kan prova att logga in eller ändra kataloger.
Åtkomst till den här sidan kräver auktorisering. Du kan prova att ändra kataloger.
GÄLLER FÖR:
MongoDB vCore
Operatorn $not
utför en logisk NOT-åtgärd för ett angivet uttryck och väljer dokument som inte matchar uttrycket.
Syntax
{ field: { $not: { <operator-expression> } } }
Parameterar
Parameter | Typ | Beskrivning |
---|---|---|
operator-expression |
Uttryck | Uttrycket för att förneka |
Exempel
Exempel 1: Grundläggande NOT-åtgärd
Hitta butiker som inte har exakt 5 heltidsanställda:
db.stores.find({
"staff.totalStaff.fullTime": {
$not: { $eq: 5 }
}
})
Utdata:
{
eventName: 'Unbeatable Bargain Bash',
promotionalDates: {
startDate: { Year: 2024, Month: 6, Day: 23 },
endDate: { Year: 2024, Month: 7, Day: 2 }
},
discounts: [
{ categoryName: 'Cabinets', discountPercentage: 8 },
{ categoryName: 'Desks', discountPercentage: 22 }
]
}
Exempel 2: INTE med reguljärt uttryck
Hitta butiker vars namn inte börjar med "First Up":
db.stores.find({
name: {
$not: /^First Up/
}
})
Utdata:
{
_id: 'cac30620-fd99-4ee2-8329-c87980ab2b24',
name: 'Contoso, Ltd. | Handbag Bargains - South Jovanny',
location: { lat: 19.6816, lon: 18.6237 },
staff: { totalStaff: { fullTime: 10, partTime: 19 } },
sales: {
totalSales: 56878,
salesByCategory: [
{ categoryName: 'Mini Bags', totalSales: 20543 },
{ categoryName: 'Satchels', totalSales: 36335 }
]
},
promotionEvents: [
{
eventName: 'Price Slash Carnival',
promotionalDates: {
startDate: { Year: 2023, Month: 6, Day: 29 },
endDate: { Year: 2023, Month: 7, Day: 8 }
},
discounts: [
{ categoryName: 'Messenger Bags', discountPercentage: 9 },
{ categoryName: 'Shoulder Bags', discountPercentage: 8 }
]
},
{
eventName: 'Major Bargain Bash',
promotionalDates: {
startDate: { Year: 2023, Month: 9, Day: 27 },
endDate: { Year: 2023, Month: 10, Day: 5 }
},
discounts: [
{ categoryName: 'Hobo Bags', discountPercentage: 15 },
{ categoryName: 'Messenger Bags', discountPercentage: 9 }
]
},
{
eventName: 'Unbeatable Bargain Bash',
promotionalDates: {
startDate: { Year: 2023, Month: 12, Day: 26 },
endDate: { Year: 2024, Month: 1, Day: 4 }
},
discounts: [
{ categoryName: 'Bucket Bags', discountPercentage: 22 },
{ categoryName: 'Shoulder Bags', discountPercentage: 10 }
]
},
{
eventName: 'Big Bargain Blitz',
promotionalDates: {
startDate: { Year: 2024, Month: 3, Day: 25 },
endDate: { Year: 2024, Month: 4, Day: 1 }
},
discounts: [
{ categoryName: 'Crossbody Bags', discountPercentage: 25 },
{ categoryName: 'Backpacks', discountPercentage: 19 }
]
},
{
eventName: 'Clearance Carnival',
promotionalDates: {
startDate: { Year: 2024, Month: 6, Day: 23 },
endDate: { Year: 2024, Month: 6, Day: 30 }
},
discounts: [
{ categoryName: 'Bucket Bags', discountPercentage: 8 },
{ categoryName: 'Mini Bags', discountPercentage: 16 }
]
},
{
eventName: 'Blowout Bargain Bash',
promotionalDates: {
startDate: { Year: 2024, Month: 9, Day: 21 },
endDate: { Year: 2024, Month: 9, Day: 29 }
},
discounts: [
{ categoryName: 'Hobo Bags', discountPercentage: 23 },
{ categoryName: 'Clutches', discountPercentage: 8 }
]
}
]
}
Exempel 3: Komplex NOT-åtgärd
Hitta butiker som inte har några kampanjevenemang med exakt 20% rabatt:
db.stores.find({
"promotionEvents.discounts.discountPercentage": {
$not: { $eq: 20 }
}
})
Utdata:
{
_id: 'f25b56da-2789-42f2-b844-3c88c7384307',
name: 'Fourth Coffee | Home Decor Corner - Kavonshire',
location: { lat: -82.8806, lon: 125.2905 },
staff: { totalStaff: { fullTime: 19, partTime: 14 } },
sales: {
totalSales: 6485,
salesByCategory: [ { categoryName: 'Picture Frames', totalSales: 6485 } ]
},
promotionEvents: [
{
eventName: 'Crazy Discount Days',
promotionalDates: {
startDate: { Year: 2024, Month: 9, Day: 21 },
endDate: { Year: 2024, Month: 10, Day: 1 }
},
discounts: [
{ categoryName: 'Mirrors', discountPercentage: 21 },
{ categoryName: 'Vases', discountPercentage: 15 }
]
}
]
}..
Begränsningar
-
$not
accepterar endast ett enda uttryck - Det går inte att direkt innehålla en annan logisk operator
Relaterat innehåll
- Granska alternativ för migrering från MongoDB till Azure Cosmos DB för MongoDB (virtuell kärna)
- Läs mer om funktionskompatibilitet med MongoDB