$ceil

The `$ceil` operator computes the ceiling of the input number. This operator returns the smallest integer value that is greater than or equal to the input.

Syntax

{ $ceil: <number> }

Parameters

<number>stringrequired

The input number whose ceiling needs to be returned. For a null or missing field, $ceil returns null.

Examples

Sample Data

{
  "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
  "sales": { "totalSales": 75670 },
  "staff": { "totalStaff": { "fullTime": 8, "partTime": 20 } }
}

Calculate the ceiling of average sales volume per employee

Calculate the ceiling of the sales volume per employee by dividing total sales by number of staff.

Query:

db.stores.aggregate([
  { $match: { _id: "40d6f4d7-50cd-4929-9a07-0a7a133c2e74" } },
  {
    $project: {
      name: 1,
      totalSales: "$sales.totalSales",
      totalStaff: { $add: ["$staff.totalStaff.fullTime", "$staff.totalStaff.partTime"] },
      ceiledAverageSalesPerStaff: {
        $ceil: {
          $divide: [
            "$sales.totalSales",
            { $add: ["$staff.totalStaff.fullTime", "$staff.totalStaff.partTime"] }
          ]
        }
      }
    }
  }
])

Output:

[
  {
    "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
    "name": "Proseware, Inc. | Home Entertainment Hub",
    "totalSales": 151864,
    "totalStaff": 39,
    "ceiledAverageSalesPerStaff": 3894
  }
]