$mod

$mod 運算子會對欄位值執行模數運算,並選取具有指定結果的文件。 此運算子有助於尋找數字欄位值除以除數後留下特定餘數的文件。 它通常用於分頁、資料採樣或在數字序列中尋找模式。

語法

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

參數

參數 Description
<field> 要對其執行模數運算的欄位。 該欄位必須包含數值。
<divisor> 用於除以欄位值的數字。 必須是正數。
<remainder> 模數運算後的預期餘數。 必須是小於除數的非負數。

範例

請參考商店集合中的此範例檔。

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

此查詢傳回的前兩個結果是:

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

此查詢傳回的前兩個結果是:

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