Created
January 18, 2019 06:35
-
-
Save 2yk/530f974acd3efe33995a8c9c291b1bb8 to your computer and use it in GitHub Desktop.
MongoDB Aggregate Example
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
const docs = await Entry.aggregate([ | |
{ $match: { compId: { $eq } } }, | |
{ $group: { _id: '$userId', data: { $push: { userId: '$userId' } }, count: { $sum: 1 } } }, | |
{ | |
$lookup: { | |
from: 'users', localField: 'data.userId', foreignField: '_id', as: 'userData', | |
}, | |
}, | |
{ $unwind: '$userData' }, | |
{ | |
$project: { | |
count: 1, | |
name: '$userData.name', | |
participant: '$userData.participant', | |
}, | |
}, | |
]); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment