What is $match in mongodb?

Docs HomeMongoDB Manual

$match

Filters the documents to pass only the documents that match the specified condition[s] to the next pipeline stage.

The $match stage has the following prototype form:

$match takes a document that specifies the query conditions. The query syntax is identical to the read operation query syntax; i.e. $match does not accept raw aggregation expressions. Instead, use a $expr query expression to include aggregation expression in $match.

  • Place the $match as early in the aggregation pipeline as possible. Because $match limits the total number of documents in the aggregation pipeline, earlier $match operations minimize the amount of processing down the pipe.

  • If you place a $match at the very beginning of a pipeline, the query can take advantage of indexes like any other db.collection.find[] or db.collection.findOne[].

  • The $match query syntax is identical to the read operation query syntax; i.e. $match does not accept raw aggregation expressions. To include aggregation expression in $match, use a $expr query expression:

    { $match: { $expr: { } } }

  • You cannot use $where in $match queries as part of the aggregation pipeline.

  • You cannot use $near or $nearSphere in $match queries as part of the aggregation pipeline. As an alternative, you can either:

    • Use $geoNear stage instead of the $match stage.

    • Use $geoWithin query operator with $center or $centerSphere in the $match stage.

  • To use $text in the $match stage, the $match stage has to be the first stage of the pipeline.

    Views do not support text search.

The examples use a collection named articles with the following documents:

{ "_id" : ObjectId["512bc95fe835e68f199c8686"], "author" : "dave", "score" : 80, "views" : 100 }
{ "_id" : ObjectId["512bc962e835e68f199c8687"], "author" : "dave", "score" : 85, "views" : 521 }
{ "_id" : ObjectId["55f5a192d4bede9ac365b257"], "author" : "ahn", "score" : 60, "views" : 1000 }
{ "_id" : ObjectId["55f5a192d4bede9ac365b258"], "author" : "li", "score" : 55, "views" : 5000 }
{ "_id" : ObjectId["55f5a1d3d4bede9ac365b259"], "author" : "annT", "score" : 60, "views" : 50 }
{ "_id" : ObjectId["55f5a1d3d4bede9ac365b25a"], "author" : "li", "score" : 94, "views" : 999 }
{ "_id" : ObjectId["55f5a1d3d4bede9ac365b25b"], "author" : "ty", "score" : 95, "views" : 1000 }

The following operation uses $match to perform a simple equality match:

db.articles.aggregate[
[ { $match : { author : "dave" } } ]
];

The $match selects the documents where the author field equals dave, and the aggregation returns the following:

{ "_id" : ObjectId["512bc95fe835e68f199c8686"], "author" : "dave", "score" : 80, "views" : 100 }
{ "_id" : ObjectId["512bc962e835e68f199c8687"], "author" : "dave", "score" : 85, "views" : 521 }

The following example selects documents to process using the $match pipeline operator and then pipes the results to the $group pipeline operator to compute a count of the documents:

db.articles.aggregate[ [
{ $match: { $or: [ { score: { $gt: 70, $lt: 90 } }, { views: { $gte: 1000 } } ] } },
{ $group: { _id: null, count: { $sum: 1 } } }
] ];

In the aggregation pipeline, $match selects the documents where either the score is greater than 70 and less than 90 or the views is greater than or equal to 1000. These documents are then piped to the $group to perform a count. The aggregation returns the following:

{ "_id" : null, "count" : 5 }

Tip

What can the $match aggregation stage be used for?

The $match stage of the pipeline can be used to filter documents so that only ones meeting certain criteria move on to the next stage. In this article, we'll discuss the $match stage in more detail and provide examples that illustrate how to perform match aggregation in MongoDB.

What is use of $Group in MongoDB?

The $group stage separates documents into groups according to a "group key". The output is one document for each unique group key. A group key is often a field, or group of fields. The group key can also be the result of an expression.

What is the use of match and group operator in MongoDB?

We can group by single as well as multiple field from the collection, we can use $group operator in MongoDB to group fields from the collection and returns the new document as result. We are using $avg, $sum, $max, $min, $push, $last, $first and $addToSet operator with group by in MongoDB.

What is $project in MongoDB?

Definition. $project. Passes along the documents with the requested fields to the next stage in the pipeline. The specified fields can be existing fields from the input documents or newly computed fields.

Chủ Đề