Skip to content

Instantly share code, notes, and snippets.

@flowl
Created February 7, 2017 14:24
Show Gist options
  • Save flowl/fd647fd9370c51049ae4cc63b7acc1e4 to your computer and use it in GitHub Desktop.
Save flowl/fd647fd9370c51049ae4cc63b7acc1e4 to your computer and use it in GitHub Desktop.
db.input.aggregate([
{
$unwind: "$attributes"
},
{
$lookup: {
from: "input",
localField: "attributes.id",
foreignField: "attributeId",
as: "attribute"
}
},
{
$addFields: {
"attribute.value": "$attributes.value"
}
},
{
$project: {
"attributes": 0
}
},
{
$unwind: "$attribute"
},
{
$group: {
_id: "$_id",
type: {
"$first": "$type"
},
productId: {
"$first": "$productId"
},
attributes: {
"$addToSet": "$attribute"
}
}
}
])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment