$ne

연산자는 $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 } }
          }
        ]
    }
]