Skip to content

Instantly share code, notes, and snippets.

@daredrum
Last active January 23, 2020 07:55
Show Gist options
  • Select an option

  • Save daredrum/dd1a1c9f1254607f103d2360fa9a4cc2 to your computer and use it in GitHub Desktop.

Select an option

Save daredrum/dd1a1c9f1254607f103d2360fa9a4cc2 to your computer and use it in GitHub Desktop.
MongoDb solutions
1. 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" }}
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment