$mod

$mod 演算子は、フィールドの値に対して剰余演算を実行し、指定された結果を持つドキュメントを選択します。 この演算子は、数値フィールドの値を除数で除算すると特定の剰余が残るドキュメントを検索する場合に役立ちます。 通常、ページネーション、データのサンプリング、または数値シーケンス内のパターンの検索に役立ちます。

構文

{
  <field>: { $mod: [ <divisor>, <remainder> ] }
}

パラメーター

パラメーター Description
<field> 剰余演算を実行するフィールド。 フィールドには数値が含まれている必要があります。
<divisor> フィールド値の除算に使用する数値。 正の数値にする必要があります。
<remainder> 剰余演算の後に必要な剰余。 除数より小さい、負でない数値にする必要があります。

例示

stores コレクションのこのサンプル ドキュメントについて考えてみましょう。

{
    "_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: 売上を 1000 で割り切れる店舗を検索する

このクエリは、売上合計が 1000 で割り切れるストアを取得します (丸い売上の数値を識別するのに役立ちます)。

db.stores.find({
  "sales.totalSales": { $mod: [1000, 0] }
}).limit(2)

このクエリによって返される最初の 2 つの結果は次のとおりです。

[
  {
    "_id": "new-store-001",
    "name": "TechWorld Electronics - Downtown Branch",
    "sales": {
      "totalSales": 5000
    },
    "createdDate": "2025-06-11T11:11:32.262Z",
    "status": "new",
    "staff": {
      "totalStaff": {
        "fullTime": 0,
        "partTime": 0
      }
    },
    "version": 1
  },
  {
    "_id": "gaming-store-mall-001",
    "name": "Gaming Paradise - Mall Location",
    "location": {
      "lat": 35.6762,
      "lon": 139.6503
    },
    "createdDate": "2025-06-11T11:13:27.180Z",
    "status": "active",
    "staff": {
      "totalStaff": {
        "fullTime": 8,
        "partTime": 12
      },
      "manager": "Alex Johnson",
      "departments": [
        "gaming",
        "accessories",
        "repairs"
      ]
    },
    "sales": {
      "totalSales": 0,
      "salesByCategory": []
    },
    "operatingHours": {
      "weekdays": "10:00-22:00",
      "weekends": "09:00-23:00"
    },
    "metadata": {
      "version": 1,
      "source": "store-management-system"
    }
  }
]

例 2: ページネーション スタイルのクエリ

このクエリは、パートタイム従業員数が 4 で除算された場合に残りの 0 を残すストアを取得します (データ サブセットの作成に役立ちます)。

db.stores.find({
  "staff.totalStaff.partTime": { $mod: [4, 0] }
})

このクエリによって返される最初の 2 つの結果は次のとおりです。

[
  {
    "_id": "new-store-001",
    "name": "TechWorld Electronics - Downtown Branch",
    "sales": {
      "totalSales": 5000
    },
    "createdDate": "2025-06-11T11:11:32.262Z",
    "status": "new",
    "staff": {
      "totalStaff": {
        "fullTime": 0,
        "partTime": 0
      }
    },
    "version": 1
  },
  {
    "_id": "gaming-store-mall-001",
    "name": "Gaming Paradise - Mall Location",
    "location": {
      "lat": 35.6762,
      "lon": 139.6503
    },
    "createdDate": "2025-06-11T11:13:27.180Z",
    "status": "active",
    "staff": {
      "totalStaff": {
        "fullTime": 8,
        "partTime": 12
      },
      "manager": "Alex Johnson",
      "departments": [
        "gaming",
        "accessories",
        "repairs"
      ]
    },
    "sales": {
      "totalSales": 0,
      "salesByCategory": []
    },
    "operatingHours": {
      "weekdays": "10:00-22:00",
      "weekends": "09:00-23:00"
    },
    "metadata": {
      "version": 1,
      "source": "store-management-system"
    }
  }
]