json - Mongodb 聚合其他集合的匹配字段的 JSON 数组字段

标签 json mongodb aggregation-framework

我是 mongodb 的新手,我有两个这样的集合:

第一个集合名称是 A

{
"_id": "1234",
"versions": [{

        "owner_id": ObjectId("100000"),
        "versions": 1,
        "type" : "info",
        "items" : ["item1","item3","item7"]


    },
    {

        "owner_id": ObjectId("100001"),
        "versions": 2,
        "type" : "bug",
        "OS": "Ubuntu",
        "Dependencies" : "Trim",
        "items" : ["item1","item7"]

    }
]}

第二个系列名称是 B

{ "_id": ObjectId("100000"), "email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="ed8c8f8ead959497c38e8280" rel="noreferrer noopener nofollow">[email protected]</a>" } { "_id": ObjectId("100001"), "email": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="98fafafbd8e0e1e2b6fbf7f5" rel="noreferrer noopener nofollow">[email protected]</a>"}

预期输出是:

{
"_id": "1234",
"versions":[{

        "owner_id": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="33525150734b4a491d505c5e" rel="noreferrer noopener nofollow">[email protected]</a>",
        "versions": 1,
        "type" : "info",
        "items" : ["item1","item3","item7"]


    },
    {

        "owner_id": "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="aecccccdeed6d7d480cdc1c3" rel="noreferrer noopener nofollow">[email protected]</a>",
        "versions": 2,
        "type" : "bug",
        "OS": "Ubuntu",
        "Dependencies" : "Trim",
        "items" : ["item1","item7"]

    }
] }

我使用了 mongo $lookup 但没有得到所需的输出 请帮忙。

谢谢!!!

最佳答案

您需要将 $unwind 版本、$lookupforeignField 上的另一个集合、$project 进行比较匹配数组中的第一个元素 $group 以原始文档格式返回

收藏一

> db.a.find()
{ "_id" : "1234", "versions" : [ { "owner_id" : "100000" }, { "owner_id" : "100001" }, { "owner_id" : "100001" } ] }

集合b

> db.b.find()
{ "_id" : "100000", "email" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="dbbab9b89ba3a2a1f5b8b4b6" rel="noreferrer noopener nofollow">[email protected]</a>" }
{ "_id" : "100001", "email" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="06646465467e7f7c2865696b" rel="noreferrer noopener nofollow">[email protected]</a>" }

聚合管道

> db.a.aggregate(
        [
            {$unwind:"$versions"},
            {$lookup : {from : "b", "localField":"versions.owner_id", "foreignField":"_id", as :"out"}}, 
            {$project : {"_id":1, "versions.owner_id":{$arrayElemAt:["$out.email",0]}}},
            {$group:{_id:"$_id", versions : {$push : "$versions"}}}
        ]   
    ).pretty()

输出

{
        "_id" : "1234",
        "versions" : [
                {
                        "owner_id" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="e6878485a69e9f9cc885898b" rel="noreferrer noopener nofollow">[email protected]</a>"
                },
                {
                        "owner_id" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="87e5e5e4c7fffefda9e4e8ea" rel="noreferrer noopener nofollow">[email protected]</a>"
                },
                {
                        "owner_id" : "<a href="https://stackoverflow.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8fededeccff7f6f5a1ece0e2" rel="noreferrer noopener nofollow">[email protected]</a>"
                }
        ]
}

关于json - Mongodb 聚合其他集合的匹配字段的 JSON 数组字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48166829/

相关文章:

javascript - MongoDB MapReduce,仅当 count > 1 时返回

mongodb错误: how do I make sure that your journal directory is mounted

java - mongodb中_id的长度有限制吗

使用多个关键字的 Jquery Grep 和 Map Json

jquery - 如何在 MVC 应用程序中返回 JSON 并循环遍历 jQuery 中返回的 json?

node.js - Express session 存储:MongoError:_id上的E11000重复键错误收集

mongodb - Mongo 查询以获取不同的嵌套文档

java - 将查询 mongo 转换为 spring Mongooperations

java - 如何向json数据添加元素

c# - 请求参数为空?