次の方法で共有


$or (ブール式)

適用対象: MongoDB 仮想コア

$or演算子は、少なくとも 1 つの式がtrueに評価されたときにtrueを返します。 式の配列に対して論理 OR 演算を実行します。 すべての式が falseと評価された場合、 $or 式全体が falseを返します。 この演算子は、複数の条件のいずれかを満たすことができる柔軟な条件を作成するのに役立ちます。

構文

$isArray演算子の構文は次のとおりです。

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

パラメーター

説明
<expression1>, <expression2>, ... 評価する 2 つ以上の式。 いずれかの式が trueと評価された場合、 $or 操作は trueを返します。

stores データセットからのサンプル json の使用方法を理解しましょう。

{
  "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f",
  "name": "First Up Consultants | Bed and Bath Center - South Amir",
  "location": {
    "lat": 60.7954,
    "lon": -142.0012
  },
  "staff": {
    "totalStaff": {
      "fullTime": 18,
      "partTime": 17
    }
  },
  "sales": {
    "totalSales": 37701,
    "salesByCategory": [
      {
        "categoryName": "Mattress Toppers",
        "totalSales": 37701
      }
    ]
  },
  "promotionEvents": [
    {
      "eventName": "Price Drop Palooza",
      "promotionalDates": {
        "startDate": {
          "Year": 2024,
          "Month": 9,
          "Day": 21
        },
        "endDate": {
          "Year": 2024,
          "Month": 9,
          "Day": 30
        }
      },
      "discounts": [
        {
          "categoryName": "Bath Accessories",
          "discountPercentage": 18
        },
        {
          "categoryName": "Pillow Top Mattresses",
          "discountPercentage": 17
        }
      ]
    }
  ]
}

例 1: 高い売上または大規模なスタッフを持つ店舗を識別する

この例では、総売上が 50,000 を超える店舗または 25 人を超えるスタッフ メンバーを検索します。

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 }
])

このクエリは、売上またはスタッフ配置の条件を満たすストアを返します。

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

例 2: 割引の対象を確認する

この例では、高い割合 (>15%) または特定の一般的なカテゴリに対して、ストアが割引を提供するかどうかを決定します。

db.stores.aggregate([
  { $match: {"_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f"} },
  {
    $project: {
      _id: 1,
      name: 1,
      hasAttractiveDealOptions: {
        $or: [
          { $gt: [{ $max: "$promotionEvents.discounts.discountPercentage" }, 15] },
          { $in: ["Bath Accessories", "$promotionEvents.discounts.categoryName"] },
          { $in: ["Mattress Toppers", "$promotionEvents.discounts.categoryName"] }
        ]
      }
    }
  }
])

このクエリでは、ストアの割引率が高いか、人気のあるカテゴリに対する取引が行われているかが確認されます。

{
  "_id": "2cf3f885-9962-4b67-a172-aa9039e9ae2f",
  "name": "First Up Consultants | Bed and Bath Center - South Amir",
  "hasAttractiveDealOptions": true
}