I have a query similar to the following;
db.getCollection('list').aggregate([
{
$facet: {
"events":[{
$match: {
'type': 'Event'
}
}],
"tasks": [{
$match: {
'type': 'Task'
}
}]
}
},
{$project: {activity:{$setUnion:['$events','$tasks']}}},
{$unwind: '$activity'},
{$replaceRoot: { newRoot: "$activity" }}
]);
One another easier approach to write this query is by using $or operator.
db.getCollection('list').aggregate([{
$match: {
$or: [{
type: 'Event'
}, {
type: 'Task'
}]
}
}]);
Source: https://gist.github.com/cthurston/7aead8229e10caa0be175babf7e8ddf1
The query that I'm working with is very similar, except I have other fields, not just 'type' going through this example. My idea is that, if I use $facet I'd get better performance if I create correct indexes for each query (that is inside $facet). On the other hand, if I use $or query as it is in the example, Mongo may struggle while trying to find the correct index for the query. Is this how it works?
Edit: It seems MongoDB can handle indexes for $or statements;
https://docs.mongodb.com/manual/reference/operator/query/or/#or-clauses-and-indexes
Best Answer
$facet - Index Use says:
$or Clauses and Indexes says:
And, your query's
$match
stage:can be simply written as:
See $or versus $in says:
Using the
$or
with proper indexing is likely to perform better than that of using the$facet
stage.