I am trying to limit the returning array to just 1, currently when I run this it shows me all the results under the added season
How do I limit the season to return 1.
{
'from': 'episode',
'let': {
'episodeId': '$_id'
},
'pipeline': [{
"$match": {
"$expr": {
"$eq": ["$show_id", "$$episodeId"]
}
}
}, {
"$sort": {
"pubDate": -1
}
}],
'as': 'season'
}
Basically all I need from the sub-array is the first season number in the picture below you can see it is season 3.
when I add the limit to the pipeline I get "Stage must be a properly formatted document."
Try this,
db.show.aggregate([
{
"$lookup": {
"from": "episode",
"let": {
"episodeId": "$_id"
},
"pipeline": [
{
"$match": {
"$expr": {
"$eq": [
"$show_id",
"$$episodeId"
]
}
}
},
{
"$sort": {
"pubDate": -1
}
},
{
$limit: 1
}
],
"as": "season"
}
}
])
You would get the out put as
[
{
"_id": 1,
"season": [
{
"_id": 2,
"pubDate": ISODate("2021-08-04T00:00:00Z"),
"show_id": 1
}
],
"show": 1
}
]