我试着只返回不到今天发布的剧集。因为我们有播客剧集,我们直到未来的日期才发布,但它们已经在DB中了。
我试过的
[{$match: {
'active': true,
'station': {
$all: ['DRN1']
}
}}, {$lookup: {
'from': 'episode',
'localField': 'url',
'foreignField': 'show_b',
pipeline: [
{
$match: {
"episode.pubDate":{$lte:new Date()}
}
}
],
'as': 'match_docs'
}}, {$project: {
'id': 1,
'title': 1,
'icon': 1,
'banner': 1,
'url': 1,
'last': {
'$slice': ['$match_docs.pubDate',-1]
},
'latestepisode': {
'$slice': ['$match_docs', -1]
}
}}, {$sort: {
'last': -1
}}]问题是输油管道。我所犯的错误是
带有“管道”的
$lookup不能指定“localField”或“foreignField”
发布于 2021-09-01 08:42:05
通过这样做才能解决问题,我知道自己是一个更好的方法,所以期待着其他的答案。
[{$match: {
'active': true,
'station': {
$all: ['DRN1']
}
}}, {$lookup: {
'from': 'episode',
'localField': 'url',
'foreignField': 'show_b',
'as': 'match_docs'
}}, {$project: {
'id': 1,
'title': 1,
'icon': 1,
'banner': 1,
'url': 1,
'match_docs':{
'$filter':{
"input" : "$match_docs",
"as" : "item",
"cond" : { "$lte" : ["$$item.pubDate",new Date()]}
}
},
'last': {
'$slice': ['$match_docs.pubDate',-1]
},
'latestepisode': {
'$slice': ['$match_docs', -1]
}
}}, {$project: {
'id': 1,
'title': 1,
'icon': 1,
'banner': 1,
'url': 1,
'last': {
'$slice': ['$match_docs.pubDate',-1]
},
'latestepisode': {
'$slice': ['$match_docs', -1]
}
}}, {$sort: {
'last': -1
}}]https://stackoverflow.com/questions/69009068
复制相似问题