Skip to content

Instantly share code, notes, and snippets.

@hubgit
Created May 16, 2020 18:43
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 hubgit/5293aab3eb4a086e5a0ea10d6a9be261 to your computer and use it in GitHub Desktop.
Save hubgit/5293aab3eb4a086e5a0ea10d6a9be261 to your computer and use it in GitHub Desktop.
MongoDB aggregate query with lookup and unwind
const items = await Item.aggregate([
{
$project: {
body: 0,
},
},
{
$lookup: {
from: 'users',
localField: 'uid',
foreignField: '_id',
as: 'user',
},
},
{ $unwind: '$user' },
]).toArray()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment