Skip to content

Instantly share code, notes, and snippets.

@zdwolfe
Created December 5, 2013 17:57
Show Gist options
  • Select an option

  • Save zdwolfe/7810162 to your computer and use it in GitHub Desktop.

Select an option

Save zdwolfe/7810162 to your computer and use it in GitHub Desktop.
db.courses.aggregate([
{
$match: {
students: "52868c8f38f0777b4e000007"
}
},
{
$unwind: "$assignments"
},
{
$group: {
_id: "$students",
assignments: {
$addToSet: "$assignments"
}
}
}
]);
{
"result" : [
{
"_id" : [
"52868c8f38f0777b4e000007"
],
"assignments" : [
{
"_id" : ObjectId("529fb8fa27a7a0af6700000a"),
"title" : "Final Basket",
"posted" : 1386199290228,
"due" : 1388964600000,
"completed" : [ ],
"description" : "The worst basket you've ever made"
},
{
"_id" : ObjectId("529fed5e7e3366530d00000c"),
"title" : "No attachments",
"posted" : 1386212702334,
"due" : 1386212663970,
"completed" : [ ],
"attachments" : [ ],
"description" : "My desc, no attachments"
},
{
"_id" : ObjectId("529fb339cc75ece5a9000009"),
"title" : "Should not see",
"posted" : 1386197817901,
"due" : 1386103628000,
"completed" : [ ],
"description" : "Due in past"
},
{
"_id" : ObjectId("529fb8e227a7a0af67000009"),
"title" : "Final Basket",
"posted" : 1386199266481,
"due" : 138896460000,
"completed" : [ ],
"description" : "The worst basket you've ever made"
},
{
"_id" : ObjectId("529faec5a64e5c6717000009"),
"title" : "Basket Case",
"posted" : 1386196677751,
"due" : 1386910800000,
"attachments" : [
{
"path" : "759a0f4dfa55bb90d8304879a57ae6ec/",
"basename" : "debian-7.2.0-i386-DVD-1.iso.torrent",
"uploadPath" : "uploads",
"_id" : ObjectId("529faec5a64e5c671700000c")
},
{
"path" : "7068add94e96bcfae46fd738581f3d45/",
"basename" : "debian-7.2.0-i386-DVD-3.iso.torrent",
"uploadPath" : "uploads",
"_id" : ObjectId("529faec5a64e5c671700000b")
},
{
"path" : "e303be466daa64bea3a96508aa4679ef/",
"basename" : "debian-7.2.0-i386-DVD-2.iso.torrent",
"uploadPath" : "uploads",
"_id" : ObjectId("529faec5a64e5c671700000a")
}
],
"description" : "Make a basket."
},
{
"_id" : ObjectId("529fad79c9866e0c63000008"),
"title" : "Test 3",
"posted" : 1386196345140,
"due" : 1386196316956,
"description" : "TEEESSTTTTING 3"
},
{
"_id" : ObjectId("528a8fb6526c429a2e00000b"),
"title" : "Nick's assignment",
"posted" : 1384812470768,
"due" : 1384812464060,
"description" : "asdfasdfasdf"
},
{
"_id" : ObjectId("529d0a17c015167c71000007"),
"title" : "Chapter 8",
"posted" : 1386023447107,
"due" : 1386046800000,
"description" : "New assignment for chapter 8"
},
{
"_id" : ObjectId("528e20c37973fe8b5e000007"),
"title" : "Zac",
"posted" : 1385046211646,
"due" : 1385046205813,
"description" : "asdfaadsa"
},
{
"_id" : ObjectId("529fa946d4465391f300000a"),
"title" : "Bee Skep Basket Project",
"posted" : 1386195270793,
"due" : 1387658828000,
"description" : "Baskets submitted that have been purchased from Michaels will not be accepted"
},
{
"_id" : ObjectId("528a8f7d526c429a2e00000a"),
"title" : "Mike's assignment",
"posted" : 1384812413179,
"due" : 1385355600000,
"description" : "asdfasdfadsfs"
}
]
}
],
"ok" : 1
}
// WHERE "_id" is the student's _id and "assignments" is the student's assignments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment