【发布时间】:2018-12-12 09:20:06
【问题描述】:
我需要帮助才能弄清楚为什么聚合函数没有按照我期望的方式响应。这是我设计的一个 RESTful API 服务,我试图在其中将集合相互连接。请注意以下几点:
收藏:季节
{
"_id": {
"$oid": "5c0fc60bfb6fc04dd6ea4e9a"
},
"Season": "1",
"TotalEpisode": "15",
"Name": null,
"Description": "First season with no name for this drama",
"PlayID": "5c0fc4aafb6fc04dd6ea4d81"
}
收藏:播放
{
"_id": {
"$oid": "5c0fc4aafb6fc04dd6ea4d81"
},
"Name": "It was the first time",
"Description": "One of the best action heros in the entertainment industry until this day",
"ReleaseDate": "24/12/2010",
"EndingDate": "12/08/2012",
"Category": "Drama"
}
我在 JavaScript 中实现的代码
function getTestLookUp(db, collectionName, response, secondCollectionName){
console.log('First collection name: ' + collectionName + '\n' + 'Second collection name: ' + secondCollectionName);
db.collection(collectionName).aggregate([
{
$lookup:
{
from: secondCollectionName,
localField: 'PlayID',
foreignField: '_id',
as: 'requestedDetails'
}
}
]).toArray((err, res) => {
if(err){
console.log(err);
} else {
console.log(res);
response.status(200).json({
'Items': res
});
}
});
}
回应
{
"Items": [
{
"_id": "5c0fc60bfb6fc04dd6ea4e9a",
"Season": "1",
"TotalEpisode": "15",
"Name": null,
"Description": "First season with no name for this drama",
"PlayID": "5c0fc4aafb6fc04dd6ea4d81",
"requestedDetails": []
}
]
}
到目前为止我检查的内容:集合名称准确,ID 也准确,因为我可以在 MLabs 搜索功能上进行搜索。我不明白为什么这会返回一个空的“requestedDetails”,因为我希望它会从 Play 集合中返回该项目。
除此之外,如果有人能指出我如何加入多个集合而不是 2 个集合,我也将不胜感激。
我欢迎就这个问题提出任何问题。
【问题讨论】:
标签: javascript database mongodb aggregate