【问题标题】:MongoDB - join 2 collections with $lookup in array of objectsMongoDB - 在对象数组中加入 2 个带有 $lookup 的集合
【发布时间】:2019-12-18 00:59:51
【问题描述】:

我在名为 fieldlist 的集合中有以下示例文档

{
    "_id" : ObjectId("5de3aafd2e46e531a45e441a"),
    "version" : 1,
    "isvalid" : true,
    "columnname_datatype" : 
    {
        "col1" : "number",
        "col2" : "string",
        "col3" : "string",
        "col4" : "date"
    },
    "last_updated_by" : ObjectId("5dc0aed15399202590f34a54")
}

在“columnname_datatype”子文档中,其键 col1、col2 等可能会根据用户输入而有所不同(本质上是动态的)。

第二个集合 fieldlistvalues 带有以下示例文档:

{
    "_id" : "test_data1",
    "name" : "Test Data version 1",
    "fieldlist_version" : ObjectId("5de3aafd2e46e531a45e441a"),
    "last_updated_date" : ISODate("2014-10-11T18:59:06.313+05:30"),
    "fieldlistarray" : [
        {
            "PKid" : "4902121027",
            "first_release_value" : 2014.67,
            "production_values" : [
                73,
                18309,
                12636
            ],
            "col1" : 13431.666666666666,
            "real_time_data" : {

            },
            "col5" : null,
            "status" : "ACTIVE",
            "col2" : "JULESBURG",
            "col3" : "EOG RES",
            "col4" : "03/15/2015"
        },
        {
            "PKid" : "4202121786",
            "first_release_value" : 14.41667,
            "olddata" : [
                204.8333333333333,
                2009.9166666666667
            ],
            "production_values" : [
                1945,
                762,
                315,
                957
            ],
            "col1" : 775.3333333333334,
            "status" : "INACTIVE",
            "col2" : "Not Available",
            "col3" : "CIRQUE, LP",
            "di_status" : "P & A",
            "col4" : "12/15/2020"
        },
    "miscdata" : [
        {
            "method" : null,
            "event_start_indices" : [
                    1,
                    10,
                    22
                ],
            "types" : "misc",
            "result" : 0
        }
        ]
    ]
}

我正在寻找以下输出

{col1: [13431.666666666666, 775.3333333333334]},
{col2: ["JULESBURG", "Not Available"]},
{col3: ["EOG RES", "CIRQUE, LP"]},
{col4: [03/15/2015, 12/15/2020]}

尝试将 $lookup 与 let & 管道直接与数组一起使用(在本例中为 fieldlistarray[]),但没有得到想要的结果。

【问题讨论】:

    标签: mongodb mongodb-query aggregation-framework


    【解决方案1】:
    db.getCollection('fieldlist').aggregate([{
      $lookup:{
        from:"fieldlistvalues",
        localField:"_id",
        foreignField:"fieldlist_version",
        as:"column_data"
      }
    },
    {
       $unwind:"$column_data"
    },
    {
       $unwind:"$column_data.fieldlistarray"
    },
    {
       $group:{
           _id:"$_id",
           col1:{$push:"$column_data.fieldlistarray.col1"},
           col2:{$push:"$column_data.fieldlistarray.col2"},
           col3:{$push:"$column_data.fieldlistarray.col3"},
           col4:{$push:"$column_data.fieldlistarray.col4"}
       }
    },
    {
        $project:{
           _id:0
    }
    }])
    

    【讨论】:

    • 它看起来应该可以工作,将尝试将结果发布回来。谢谢你。此外,如果我在 fieldlist 集合中的字段/键名称从 col1、2、3.. 更改为 col20,col21 我们可以使用 $column_data.fieldlistarray.k 以某种方式使上述动态变化
    猜你喜欢
    • 1970-01-01
    • 2020-08-14
    • 2016-06-19
    • 1970-01-01
    • 2019-01-28
    • 1970-01-01
    • 2021-03-10
    相关资源
    最近更新 更多