The pipeline provides efficient data aggregation using native operations within MongoDB, and is the preferred method for data aggregation in MongoDB. The aggregation pipeline can operate on a sharded collection. The aggregation pipeline can use indexes to improve its performance during some of its stages.
The MongoDB aggregation framework is an extremely powerful set of tools. The processing is done on the server itself which results in less data being sent over the network.
MongoDB Aggregation goes further though and can also perform relational-like joins, reshape documents, create new and update existing collections, and so on. While there are other methods of obtaining aggregate data in MongoDB, the aggregation framework is the recommended approach for most work.
The MongoDB $match operator filters the documents to pass only those documents that match the specified condition(s) to the next pipeline stage.
$match: { $or: [{ author: 'dave' }, { author: 'john' }] }
Like so, since the $match
operator just takes what you would normally put into the find()
function
In this particular case, where you are $or
-ing the same field, the $in
operator would be a better choice, also because it increases readability:
$match: {
'author': {
$in: ['dave','john']
}
}
According to the MongoDB docs, using $in
is recommended in this case:
$or versus $in
When using $or with <expressions> that are equality checks for the value of the same field, use the $in operator instead of the $or operator.
https://docs.mongodb.com/manual/reference/operator/query/or/#or-versus-in
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