$minN

The `$minN` operator is used to retrieve the bottom N values for a field based on a specified filtering criteria. It's useful for identifying the lowest values in a dataset.

Syntax

{
  $minN: {
    input: <field or expression>,
    n: <number of values to retrieve>
  }
}

Parameters

inputobjectrequired

Specifies the field or expression to evaluate for minimum values.

nnumberrequired

Specifies the number of minimum values to retrieve. Must be a positive integer.

Examples

Sample Data

{
  "_id": "7954bd5c-9ac2-4c10-bb7a-2b79bd0963c5",
  "name": "Lakeshore Retail | DJ Equipment Stop",
  "sales": {
    "salesByCategory": [
      { "categoryName": "DJ Headphones", "totalSales": 35921 }
    ],
    "fullSales": 3700
  }
}

Retrieve bottom two sales categories

Retrieve the bottom two sales categories per store with the lowest sales volume.

This query uses the $minN operator on the nested salesCategory field.

Query:

db.stores.aggregate([
  {
    $project: {
      bottomSalesCategories: {
        $minN: {
          input: "$sales.salesByCategory",
          n: 2
        }
      }
    }
  },
  { $limit: 4 }
])

Output:

[
  {
    "_id": "a715ab0f-4c6e-4e9d-a812-f2fab11ce0b6",
    "bottomSalesCategories": [
      { "categoryName": "Stockings", "totalSales": 25731 }
    ]
  },
  {
    "_id": "923d2228-6a28-4856-ac9d-77c39eaf1800",
    "bottomSalesCategories": [
      { "categoryName": "Lamps", "totalSales": 19880 },
      { "categoryName": "Rugs", "totalSales": 20055 }
    ]
  }
]

Using $minN as array-expression

Extract the two lowest sales values for a specific store document.

This query uses $minN as an array expression to get the lowest two sales values.

Query:

db.stores.aggregate([
  { $match: {_id: "40d6f4d7-50cd-4929-9a07-0a7a133c2e74"} },
  {
    $project: {
      name: 1,
      lowestTwoSales: {
        $minN: {
          input: "$sales.salesByCategory.totalSales",
          n: 2
        }
      }
    }
  }
])

Output:

[
  {
    "_id": "40d6f4d7-50cd-4929-9a07-0a7a133c2e74",
    "name": "Proseware, Inc. | Home Entertainment Hub",
    "lowestTwoSales": [28946, 3000]
  }
]

Related