Skip to content

Instantly share code, notes, and snippets.

@2yk
Created January 18, 2019 06:35
Show Gist options
  • Save 2yk/530f974acd3efe33995a8c9c291b1bb8 to your computer and use it in GitHub Desktop.
Save 2yk/530f974acd3efe33995a8c9c291b1bb8 to your computer and use it in GitHub Desktop.
MongoDB Aggregate Example
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