Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MongoDB aggregation framework match OR

People also ask

Which aggregation method is preferred for use by MongoDB?

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.

Is aggregation a framework in MongoDB?

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.

Is aggregation good in MongoDB?

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.

What is the use of match in MongoDB?

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