$first

The `$first` operator sorts documents on one or more fields specified by the query and returns the first document matching the filtering criteria. If no sorting order is specified, the order is undefined.

Syntax

{
  $first: <expression>
}

Parameters

expressionobjectrequired

The expression to evaluate and return the first document from the result set.

Examples

Sample Data

{
  "_id": "0fcc0bf0-ed18-4ab8-b558-9848e18058f4",
  "name": "First Up Consultants | Beverage Shop",
  "lastUpdated": "2025-06-11T10:48:01.291Z",
  "sales": {
    "salesByCategory": [
      { "categoryName": "Wine Accessories" }
    ]
  }
}

Get the least recently updated document

Retrieve the least recently updated store by sorting in ascending order and taking the first document.

This query sorts stores by lastUpdated field and returns the first (oldest) update timestamp.

Query:

db.stores.aggregate([
  {
    $match: { company: { $in: ["First Up Consultants"] } }
  },
  {
    $sort: { lastUpdated: 1 }
  },
  {
    $group: {
      _id: "$company",
      firstUpdated: { $first: "$lastUpdated" }
    }
  }
])

Output:

[
  {
    "_id": "First Up Consultants",
    "firstUpdated": "2025-06-11T10:48:01.291Z"
  }
]

Get first category by sales amount per store

Retrieve the first category alphabetically within each store.

This query sorts categories alphabetically and returns the first one per store.

Query:

db.stores.aggregate([
  { $unwind: "$sales.salesByCategory" },
  {
    $sort: {
      "_id": 1,
      "sales.salesByCategory.categoryName": 1
    }
  },
  {
    $group: {
      _id: "$_id",
      storeName: { $first: "$name" },
      firstCategory: {
        $first: {
          categoryName: "$sales.salesByCategory.categoryName",
          categoryTotalSales: "$sales.salesByCategory.totalSales"
        }
      }
    }
  }
])

Output:

[
  {
    "_id": "64ec6589-068a-44a6-be5b-9d37bb0a90f1",
    "storeName": "First Up Consultants | Computer Gallery",
    "firstCategory": {
      "categoryName": "Cases",
      "categoryTotalSales": 36386
    }
  }
]

Related