Skip to content

Instantly share code, notes, and snippets.

@zdwolfe
Created December 3, 2013 00:24
Show Gist options
  • Save zdwolfe/7761740 to your computer and use it in GitHub Desktop.
Save zdwolfe/7761740 to your computer and use it in GitHub Desktop.
Get all assignments given a user's ID
db.courses.aggregate([{
$match: {
students: "52868c8f38f0777b4e000007"
}
},
{
$unwind: "$assignments"
},
{
$group: {
_id: "$assignments"
}
},
{
$project: {
assignment: "$_id",
_id: false
}
}
]);
{
"result" : [
{
"assignment" : {
"_id" : ObjectId("529d0a17c015167c71000007"),
"title" : "Chapter 8",
"posted" : 1386023447107,
"due" : 1386046800000,
"description" : "New assignment for chapter 8"
}
},
{
"assignment" : {
"_id" : ObjectId("528e20c37973fe8b5e000007"),
"title" : "Zac",
"posted" : 1385046211646,
"due" : 1385046205813,
"description" : "asdfaadsa"
}
},
{
"assignment" : {
"_id" : ObjectId("528a8fb6526c429a2e00000b"),
"title" : "Nick's assignment",
"posted" : 1384812470768,
"due" : 1384812464060,
"description" : "asdfasdfasdf"
}
},
{
"assignment" : {
"_id" : ObjectId("528a8f7d526c429a2e00000a"),
"title" : "Mike's assignment",
"posted" : 1384812413179,
"due" : 1385355600000,
"description" : "asdfasdfadsfs"
}
}
],
"ok" : 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment