【问题标题】:Having issues merging documents from two collections in MongoDB在 MongoDB 中合并来自两个集合的文档时遇到问题
【发布时间】:2017-07-13 06:49:21
【问题描述】:

我正在一个集合中的 mongoDB 中创建一个 country->locations 文档。

然后,我将创建一个单独的临时集合作为 ETL 作业(部门->员工)的一部分。

然后我想在 location_id 上的匹配项上插入与位置相关的部门。

这是位置文件(国家->位置):

这里是临时集合中的部门文档(department->employees):

我想遍历每个位置记录,然后找到匹配的部门并将它们添加到位置内的“部门”数组中,但我很挣扎:

到目前为止的JavaScript是:

function stuffDepartmentsIntoLocations() {
    var ops = [];

        var departments = function() {
        this.deptsArray = [];
    };

    db.HR.find().forEach(function(country) 
        {
            for(var i=0;i < country.locations.length; i++) {

                              departments.deptsArray= [];

                db.tempDEPTS.find({"LOCATION_ID":country.locations[i].LOCATION_ID}).forEach(function(dept) 
                {
                    departments.deptsArray.push(dept);
                                });

                db.tempDEPTS.find({"LOCATION_ID":country.locations[i].LOCATION_ID}).forEach(function(dept) 
                    {
                        ops.push(
                            {
                                    "updateOne": 
                                {
                                        "filter": {"_id": country._id },
                                        "update": {"$push": {"locations": {$each: [] ,$position: i }}}
                                        }
                                } 
                            );
                        if ( ops.length == 1000 ) {
                                db.HR.bulkWrite(ops,{ "ordered": false });
                                ops = [];
                        }
                    }               
                );          
             }      
      }
    );

   if ( ops.length > 0 ) {
     db.HR.bulkWrite(ops,{ "ordered": false });
   }

 return true;
}

但这不起作用,我没有看到任何插入。

我想要得到的是相关位置(与 LOCATION_ID 匹配)内的新“部门”数组,然后将 1 个或多个部门文档插入到数组中。即类似这样的东西(简化为理解)。

但是您可以看到创建了一个部门数组,其中插入了临时集合中的所有部门文档(“部门”需要是一个数组,即使只有一个部门)。

任何建议表示赞赏。

【问题讨论】:

  • 我怀疑我应该使用 $lookup 或者 $out

标签: mongodb


【解决方案1】:

我实际上使用 $lookup 和 $out 非常接近,但并不完全如此。

如果我创建了 2 个临时集合,则通过查找将它们加入并输出到最终集合中:

{
    db.tempLOCATIONS.aggregate([
    {
            $unwind: "$locations"
    },
        {
            $lookup:
                {
                    from: "tempDEPTS",
                    localField: "locations.LOCATION_ID",
                    foreignField: "LOCATION_ID",
                    as: "locations.departments"
                }
     },
         { 
        $group : { _id : {COUNTRY_ID:"$COUNTRY_ID", COUNTRY_NAME:"$COUNTRY_NAME"}, locations: { $push: "$locations" }}

     },
         {
        $out:  "HR"
         }

])
}

我在下面得到这个。唯一的问题是我不希望我的“_id”字段是“COUNTRY_ID”和“COUNTRY_NAME”的复合,我只想要一个普通的“_id”,以及“COUNTRY_ID”和“COUNTRY_NAME”作为根:

【讨论】:

    猜你喜欢
    • 2017-10-17
    • 2021-10-30
    • 1970-01-01
    • 2020-12-12
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 1970-01-01
    • 2019-12-29
    相关资源
    最近更新 更多