Last active
January 23, 2020 07:55
-
-
Save daredrum/dd1a1c9f1254607f103d2360fa9a4cc2 to your computer and use it in GitHub Desktop.
MongoDb solutions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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