$match
The $match pipeline stage in Amazon DocumentDB is used to filter the input documents to only those that match the specified query criteria. It is one of the most commonly used pipeline stages in aggregation operations. The $match stage is applied before any other pipeline stages, allowing you to efficiently reduce the number of documents that need to be processed by the subsequent stages.
Parameters
-
query: A document that expresses the selection criteria for the operation. The query document uses the same syntax as thefind()method.
Example (MongoDB Shell)
The following example demonstrates the use of the $match stage to filter documents based on a specific field value.
Create sample documents
db.collection.insertMany([ { _id: 1, name: "John", age: 25, city: "New York" }, { _id: 2, name: "Jane", age: 30, city: "Los Angeles" }, { _id: 3, name: "Bob", age: 35, city: "Chicago" }, { _id: 4, name: "Alice", age: 40, city: "Miami" } ]);
Query example
db.collection.aggregate([ { $match: { age: { $gt: 30 } } }, { $project: { _id: 1, name: 1, city: 1 } } ]);
Output
[
{ "_id": 3, "name": "Bob", "city": "Chicago" },
{ "_id": 4, "name": "Alice", "city": "Miami" }
]
The $match stage filters the documents to include only those where the age field is greater than 30.
Code examples
To view a code example for using the $match command, choose the tab for the language that you want to use: