Aracılığıyla paylaş


$and

$and işleci bir dizi ifade üzerinde mantıksal AND işlemi gerçekleştirir ve tüm ifadeleri karşılayan belgeleri alır.

Sözdizimi

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

Parametreler

Parametre Description
expression Bir belgenin sonuçlara eklenmesi için tümü 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: AND işlecini mantıksal sorgu olarak kullanma

Bu sorgu, tam zamanlı çalışan sayısının 10'dan fazla ve yarı zamanlı çalışanların işlecini kullanarak $and 15'ten az olduğu mağazaları filtreler. Yalnızca name ve staff alanlarını projeler ve sonucu üç kayıtla sınırlar.

db.stores.find({
    $and: [{
        "staff.employeeCount.fullTime": {
            $gt: 10
        }
    }, {
        "staff.employeeCount.partTime": {
            $lt: 15
        }
    }]
}, {
    "name": 1,
    "staff": 1
}).limit(3)

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

[
    {
        "_id": "e60c807b-d31c-4903-befb-5d608f260ba3",
        "name": "Wide World Importers | Appliance Emporium - Craigfort",
        "staff": {
            "totalStaff": {
                "fullTime": 11,
                "partTime": 8
            }
        }
    },
    {
        "_id": "70032165-fded-47b4-84a3-8d9c18a4d1e7",
        "name": "Northwind Traders | Picture Frame Bazaar - Lake Joesph",
        "staff": {
            "totalStaff": {
                "fullTime": 14,
                "partTime": 0
            }
        }
    },
    {
        "_id": "dda2a7d2-6984-40cc-bbea-4cbfbc06d8a3",
        "name": "Contoso, Ltd. | Home Improvement Closet - Jaskolskiview",
        "staff": {
            "totalStaff": {
                "fullTime": 16,
                "partTime": 8
            }
        }
    }
]

Örnek 2: Yüksek satış ve yeterli personele sahip mağazaları bulmak için boole ifadesi olarak AND işlecini kullanın

Bu sorgu, hem toplam satışları 100.000'den büyük hem de toplam personel üyesi sayısı 30'dan fazla olan depoları bulur.

db.stores.aggregate([
  {
    $project: {
      name: 1,
      totalSales: "$sales.totalSales",
      totalStaff: {
        $add: ["$staff.employeeCount.fullTime", "$staff.employeeCount.partTime"]
      },
      meetsHighPerformanceCriteria: {
        $and: [
          { $gt: ["$sales.totalSales", 100000] },
          { $gt: [{ $add: ["$staff.employeeCount.fullTime", "$staff.employeeCount.partTime"] }, 30] }
        ]
      }
    }
  },
  { $limit: 2 }
])

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

[
 {
    "_id": "905d1939-e03a-413e-a9c4-221f74055aac",
    "name": "Trey Research | Home Office Depot - Lake Freeda",
    "totalStaff": 31,
    "meetsHighPerformanceCriteria": false
  },
  {
    "_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6",
    "name": "Lakeshore Retail | Holiday Supply Hub - Marvinfort",
    "totalStaff": 27,
    "meetsHighPerformanceCriteria": false
  }
]