So I have a MongoDB document that looks like so:
Awards
:
[
{
"year": "2017",
"winners": [
{
"name": "james",
"id": 1
},
{
"name": "adam",
"id": 2
}
]
},
{
"year": "2018",
"winners": [
{
"name": "mary",
"id": 3
},
{
"name": "jane",
"id": 4
}
]
}
]
And I'm basically trying to expand out the winners from each year so that the schema is roughly (year, winner_id)
i can later use that for a join I plan on doing. Something like this:
[
{
"year":"2017",
"winner_id":1
},
{
"year":"2017",
"winner_id":2
},
{
"year":"2018",
"winner_id":3
},
{
"year":"2018",
"winner_id":4
}
]
so intuitively, I'm looking for something like $unwind
, but the problem is that winners
is not an array.
I tried db.awards.aggregate({'$unwind':'$winners.id'})
but that didn't work. (gave an empty result)
So afterwards I thought db.awards.aggregate({'$unwind': {'$objectToArray':'$winners.id'}})
but that didn't work either. (gave an error)
According to this post I should do this:
db.awards.aggregate(
{ '$project': {
_id: 1,
'winners': 1
} },
{'$unwind': '$winners'},
{'$unwind': '$winners.id'}
)
but even that didn't work, I still get the embedded document for each winner
Any thoughts?
Your data is invalid JSON. In assume that winners
attribute is very likely an array.
I can see three additional issues in your aggregation commands:
aggregate
function.$project
is different in aggregation pipelines. You do not define which fields should be included. You do define the actual mappingwinners
array.I think it is easier to do the $unwind
before projection
db.awards.aggregate(
[
{
$unwind: { path: '$winners' }
},
{
$project: {
year: '$year',
winner_id: '$winners.id'
}
}
]
);