mongodb 管道限制为 1
mongodb pipeline limit to 1
我正在尝试将 returning 数组限制为 1,目前当我 运行 它显示了添加的 season
下的所有结果 我如何限制赛季到 return 1.
{
'from': 'episode',
'let': {
'episodeId': '$_id'
},
'pipeline': [{
"$match": {
"$expr": {
"$eq": ["$show_id", "$$episodeId"]
}
}
}, {
"$sort": {
"pubDate": -1
}
}],
'as': 'season'
}
基本上我需要的子数组就是下图中的第一个季节编号,你可以看到它是第 3 季。
当我向管道添加限制时,我得到“阶段必须是格式正确的文档”。
试试这个,
db.show.aggregate([
{
"$lookup": {
"from": "episode",
"let": {
"episodeId": "$_id"
},
"pipeline": [
{
"$match": {
"$expr": {
"$eq": [
"$show_id",
"$$episodeId"
]
}
}
},
{
"$sort": {
"pubDate": -1
}
},
{
$limit: 1
}
],
"as": "season"
}
}
])
你会得到输出
[
{
"_id": 1,
"season": [
{
"_id": 2,
"pubDate": ISODate("2021-08-04T00:00:00Z"),
"show_id": 1
}
],
"show": 1
}
]
我正在尝试将 returning 数组限制为 1,目前当我 运行 它显示了添加的 season
下的所有结果 我如何限制赛季到 return 1.
{
'from': 'episode',
'let': {
'episodeId': '$_id'
},
'pipeline': [{
"$match": {
"$expr": {
"$eq": ["$show_id", "$$episodeId"]
}
}
}, {
"$sort": {
"pubDate": -1
}
}],
'as': 'season'
}
基本上我需要的子数组就是下图中的第一个季节编号,你可以看到它是第 3 季。
当我向管道添加限制时,我得到“阶段必须是格式正确的文档”。
试试这个,
db.show.aggregate([
{
"$lookup": {
"from": "episode",
"let": {
"episodeId": "$_id"
},
"pipeline": [
{
"$match": {
"$expr": {
"$eq": [
"$show_id",
"$$episodeId"
]
}
}
},
{
"$sort": {
"pubDate": -1
}
},
{
$limit: 1
}
],
"as": "season"
}
}
])
你会得到输出
[
{
"_id": 1,
"season": [
{
"_id": 2,
"pubDate": ISODate("2021-08-04T00:00:00Z"),
"show_id": 1
}
],
"show": 1
}
]