$first
New from version 5.0.
Not supported by Elastic cluster.
The $first operator in Amazon DocumentDB returns the first document from a grouped set of documents. It is commonly used in aggregation pipelines to retrieve the first document that matches a specific condition.
Parameters
-
expression: The expression to return as the first value in each group.
Example (MongoDB Shell)
The following example demonstrates the use of the $first operator to retrieve the first item value encountered for each category during the aggregation.
Note: $first returns the first document based on the current order of documents in the pipeline. To ensure a specific order (e.g., by date, price, etc.), a $sort stage should be used before the $group stage.
Create sample documents
db.products.insertMany([ { _id: 1, item: "abc", price: 10, category: "food" }, { _id: 2, item: "jkl", price: 20, category: "food" }, { _id: 3, item: "xyz", price: 5, category: "toy" }, { _id: 4, item: "abc", price: 5, category: "toy" } ]);
Query example
db.products.aggregate([ { $group: { _id: "$category", firstItem: { $first: "$item" } } } ]);
Output
[
{ "_id" : "food", "firstItem" : "abc" },
{ "_id" : "toy", "firstItem" : "xyz" }
]
Code examples
To view a code example for using the $first command, choose the tab for the language that you want to use: