Skip to content

Instantly share code, notes, and snippets.

@mewben
Created October 5, 2018 01:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mewben/4f6883f5fd97949c60aae7f699793995 to your computer and use it in GitHub Desktop.
Save mewben/4f6883f5fd97949c60aae7f699793995 to your computer and use it in GitHub Desktop.
Query with $lookup count
Site.aggregate()
.lookup({
from: 'pages',
localField: '_id',
foreignField: 'siteID',
as: 'pages',
})
.addFields({ pageCount: { $size: '$pages' } }) // add pageCount field
.project({ pages: 0 }) // exclude pages array
.exec();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment