【问题标题】:MongoDB aggregate function is not returning the value of collection joined using JavaScriptMongoDB 聚合函数未返回使用 JavaScript 加入的集合的值
【发布时间】: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


    【解决方案1】:

    在研究这个问题时,我无意中遇到了另一个问题,其中有人写了一条评论,指出“您可能正在将字符串与 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"
                    }
                ]
            }
        ]
    }
    

    【讨论】:

      猜你喜欢
      • 2018-04-13
      • 2019-05-16
      • 2022-10-13
      • 2017-02-28
      • 2021-07-28
      • 2015-07-23
      • 2016-07-04
      • 2011-03-20
      • 1970-01-01
      相关资源
      最近更新 更多