Aracılığıyla paylaş


$ veya

işleci $or , bir ifade dizisi üzerinde mantıksal VEYA işlemi gerçekleştirir ve belirtilen koşullardan en az birini karşılayan belgeleri alır.

Sözdizimi

{
    $or: [{
        < expression1 >
    }, {
        < expression2 >
    }, ..., {
        < expressionN >
    }]
}

Parametreler

Parametre Description
expression Belgenin dahil edilmesi için en az birinin doğru olması gereken ifade dizisi

Örnekler

Stores koleksiyonundaki bu örnek belgeyi göz önünde bulundurun.

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

Örnek 1: MANTıKSAL sorgu olarak OR işlemini kullanma

Bu sorgu, 15'ten fazla tam zamanlı personeli veya 20'den fazla yarı zamanlı personeli olan depoları alır, her iki koşulda da $or işlecini kullanarak bir sorgu çalıştırır. Ardından, sonuç kümesindeki mağazalardan yalnızca ad ve personel alanlarını yansıtın.

db.stores.find(
  {
    $or: [
      { "staff.employeeCount.fullTime": { $gt: 15 } },
      { "staff.employeeCount.partTime": { $gt: 20 } }
    ]
  },
  {
    "name": 1,
    "staff": 1
  }
).limit(2)

Bu sorgu tarafından döndürülen ilk iki sonuç şunlardır:

[
  {
    "_id": "dda2a7d2-6984-40cc-bbea-4cbfbc06d8a3",
    "name": "Contoso, Ltd. | Home Improvement Closet - Jaskolskiview",
    "staff": {
      "employeeCount": {
        "fullTime": 16,
        "partTime": 8
      }
    }
  },
  {
    "_id": "44fdb9b9-df83-4492-8f71-b6ef648aa312",
    "name": "Fourth Coffee | Storage Solution Gallery - Port Camilla",
    "staff": {
      "employeeCount": {
        "fullTime": 17,
        "partTime": 15
      }
    }
  }
]

Örnek 2: Yüksek satış veya büyük personele sahip mağazaları tanımlamak için boole ifadesi olarak OR işlecini kullanma

Bu sorgu, toplam satışları 50.000'den fazla veya toplam personel üyesi sayısı 25'ten fazla olan depoları alır.

db.stores.aggregate([
  {
    $project: {
      name: 1,
      totalSales: "$sales.totalSales",
      totalStaff: { 
        $add: ["$staff.employeeCount.fullTime", "$staff.employeeCount.partTime"] 
      },
      qualifiesForProgram: {
        $or: [
          { $gt: ["$sales.totalSales", 50000] },
          { $gt: [{ $add: ["$staff.employeeCount.fullTime", "$staff.employeeCount.partTime"] }, 25] }
        ]
      }
    }
  },
  { $limit: 4 }
])

Bu sorgu tarafından döndürülen ilk dört sonuç şunlardır:

[
  {
    "_id": "905d1939-e03a-413e-a9c4-221f74055aac",
    "name": "Trey Research | Home Office Depot - Lake Freeda",
    "totalStaff": 31,
    "qualifiesForProgram": true
  },
  {
    "_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6",
    "name": "Lakeshore Retail | Holiday Supply Hub - Marvinfort",
    "totalStaff": 27,
    "qualifiesForProgram": true
  },
  {
    "_id": "923d2228-6a28-4856-ac9d-77c39eaf1800",
    "name": "Lakeshore Retail | Home Decor Hub - Franciscoton",
    "totalStaff": 13,
    "qualifiesForProgram": false
  },
  {
    "_id": "7e53ca0f-6e24-4177-966c-fe62a11e9af5",
    "name": "Contoso, Ltd. | Office Supply Deals - South Shana",
    "totalStaff": 2,
    "qualifiesForProgram": false
  }
]

PerformansLa İlgili Dikkat Edilmesi Gerekenler

  • Daha iyi performans bulmak için önerileri gözden geçirin.
    • Dizideki $or her koşul bağımsız olarak değerlendirilir
    • Daha iyi performans için mümkün olduğunda dizinleri kullanma
    • En iyi yürütme için koşulların sırasını göz önünde bulundurun
    • Aynı alanda birden çok eşitlik denetimi yerine $in kullanın $or
    • Koşulların sayısını $or makul tutun