MongoDB aggregation framework match OR
Is it possible to do an OR in the $match?
I mean something like this:
db.articles.aggregate(
{ $or: [ $match : { author : "dave" }, $match : { author : "john" }] }
);
$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