Skip to content

Instantly share code, notes, and snippets.

/query.js Secret
Created Oct 31, 2016

Embed
What would you like to do?
db.votes.aggregate([
{
$lookup: {
from: "users",
localField: "createdBy",
foreignField: "_id",
as: "user"
}
},
{
$unwind: "$user"
},
{
$unwind: "$user.emails"
},
{
$sort: { "user.emails.verified": -1 }
},
{
$group: {
_id: "$_id",
createdBy: { $first: "$createdBy" },
fellowId: { $first: "$fellowId" },
user: { $first: "$user" }
}
},
{
$lookup: {
from: "fellows",
localField: "fellowId",
foreignField: "_id",
as: "fellow"
}
},
{
$unwind: "$fellow"
},
{
$lookup: {
from: "companies",
localField: "companyId",
foreignField: "_id",
as: "company"
}
},
{
$unwind: "$company"
},
{
$lookup: {
from: "teams",
localField: "teamId",
foreignField: "_id",
as: "team"
}
},
{
$unwind: "$team"
},
{
$project: {
"_id": 1,
"firstName": "$user.profile.name.first",
"lastName": "$user.profile.name.last",
"emailAddress": "$user.emails.address",
"companyTitle": "$company._id",
"teamTitle": "$team._id",
"fellowTitle": "$fellow._id",
"isVerified": "$user.emails.verified"
}
},
{
$out: "results"
}
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.