Skip to content

Instantly share code, notes, and snippets.

@cthurston
Created November 3, 2017 14:21
Show Gist options
  • Star 41 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save cthurston/7aead8229e10caa0be175babf7e8ddf1 to your computer and use it in GitHub Desktop.
Save cthurston/7aead8229e10caa0be175babf7e8ddf1 to your computer and use it in GitHub Desktop.
MongoDb combine $facet results into a single result set.
db.getCollection('list').aggregate([
{
$facet: {
"events":[{
$match: {
'type': 'Event'
}
}],
"tasks": [{
$match: {
'type': 'Task'
}
}]
}
},
{$project: {activity:{$setUnion:['$events','$tasks']}}},
{$unwind: '$activity'},
{$replaceRoot: { newRoot: "$activity" }}
]);
@jawadwaseem-10p
Copy link

How would you apply skip and limit on the combined results?

@shailOverjet
Copy link

How would you apply skip and limit on the combined results?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment