MongoDB聚合函数未返回使用JavaScript连接的集合的值

时间:2018-12-12 09:20:07

标签: javascript database mongodb aggregate

我需要帮助才能弄清楚为什么聚合函数没有按照我期望的方式响应。这是我设计的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,我也将不胜感激。

欢迎您对此问题提出任何疑问。

1 个答案:

答案 0 :(得分:0)

在继续研究这个问题的同时,我偶然遇到了另一个问题,有人在其中写了一条评论,指出“您可能正在将String与ObjectID进行比较”。这是导致此错误的原因,因为我从数据库中获取了一个String变量作为回报,并且我正在将String变量与_id进行比较,该_id期望看到一个ObjectID变量来完成查询。因此,这意味着我的查询/查找从未匹配这两个变量。

解决此问题的唯一方法是进行转换(从字符串到ObjectID),然后比较值。但是,由于我使用的是MongoDB的^ 3.1.10版本,因此无法使用此功能。需要将版本更新为4.0才能实现此功能。

为了解决此问题,我设法将外部ID括在$ iod标签内。

之前

{
"_id": {
    "$oid": "5c0fc60bfb6fc04dd6ea4e9a"
},
"Season": "1",
"TotalEpisode": "15",
"Name": null,
"Description": "First season with no name for this drama",
"PlayID": "5c0fc4aafb6fc04dd6ea4d81"
}

之后

{
"_id": {
    "$oid": "5c0fc60bfb6fc04dd6ea4e9a"
},
"Season": "1",
"TotalEpisode": "15",
"Name": null,
"Description": "First season with no name for this drama",
"PlayID": {
"$oid": "5c0fc4aafb6fc04dd6ea4d81"
}
}

回复

{
    "Items": [
        {
            "_id": "5c0fc60bfb6fc04dd6ea4e9a",
            "Season": "1",
            "TotalEpisode": "15",
            "Name": null,
            "Description": "First season with no name for this drama",
            "PlayID": "5c0fc4aafb6fc04dd6ea4d81",
            "Details": [
                {
                    "_id": "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"
                }
            ]
        }
    ]
}