I'm new to MongoDB and to NoSQL servers in general. I'm testing some stuffs (insert in local DB, easy queries, etc..) on my computer with Studio 3T software (IDE for MongoDB) and I got into these 3 operators while testing aggregate method: $project, $filter and $match. The problem is I don't get the differences between these operators, the only thing I think i might understood is that $project is used to "choose" which fields (or whole array) to show on screen in response from the query, but what about $match and $filter? they seem to do the same thing. I read MongoDB documentation but it doesn't explain the operators sufficiently detailed (in my opinion of course).
Thanks in advance
$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 .
Definition. $filter. Selects a subset of an array to return based on the specified condition. Returns an array with only those elements that match the condition. The returned elements are in the original order.
The $project takes a document that can specify the inclusion of fields, the suppression of the _id field, the addition of new fields, and the resetting of the values of existing fields. Alternatively, you may specify the exclusion of fields. The $project specifications have the following forms: Form. Description.
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.
$project and $match are Aggregation Pipeline Stages and $filter is an Aggregation Pipeline Operator.
Difference between Pipeline stage and Pipeline operator is a stage can used separately, whereas a Pipeline operator can be used only inside a Pipeline stage.
$project is used to project the elements to next stage
$match is used to filter the collection to give only the matching documents for the input query/criteria and it is advisable to use it at the start of Aggregation Pipeline before using other operators. Since it filters the collection to a reduced number of documents and only reduced set of documents is passed to the next stage of Aggregation Pipeline.
$filter is used in arrays, it selects a subset of an array to return based on the specified condition. Returns an array with only those elements that match the condition. The returned elements are in the original order.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With