Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json MongoDB中两个集合的聚合_Json_Mongodb_Mongodb Query_Aggregation Framework - Fatal编程技术网

Json MongoDB中两个集合的聚合

Json MongoDB中两个集合的聚合,json,mongodb,mongodb-query,aggregation-framework,Json,Mongodb,Mongodb Query,Aggregation Framework,我有两个集合,我正在尝试查找它们。我不是100%确定如何接近它 系列1:员工 { "data": [ { "full name": "Keith Richards", "age": 21, "userName": "keith1@keith.com", "employeeDetails": { "id": 102522 } }, { "full name": "Jim Morrison", "age"

我有两个集合,我正在尝试查找它们。我不是100%确定如何接近它

系列1:员工

{
 "data": [
  {
     "full name": "Keith Richards",
     "age": 21,
     "userName": "keith1@keith.com",
     "employeeDetails": {
        "id": 102522
    }
  },
  {
    "full name": "Jim Morrison",
     "age": 27,
     "userName": "jim@jim.com",
     "employeeDetails": {
        "id": 135522
    }
  }
 ]
}
{
 "data": [
  {
     "dateCreated": "02-04-2016",
     "billable": true,
     "minutes": 150,
     "employeeId": {
        "id": 102522
        }
  },
  {
     "dateCreated": "03-04-2016",
     "billable": true,
     "minutes": 250,
     "employeeId": {
        "id": 102522
   }
  },
  {
     "dateCreated": "04-04-2016",
     "billable": true,
     "minutes": 20,
     "employeeId": {
        "id": 135522
   }
  }
]
}
集合2:条目

{
 "data": [
  {
     "full name": "Keith Richards",
     "age": 21,
     "userName": "keith1@keith.com",
     "employeeDetails": {
        "id": 102522
    }
  },
  {
    "full name": "Jim Morrison",
     "age": 27,
     "userName": "jim@jim.com",
     "employeeDetails": {
        "id": 135522
    }
  }
 ]
}
{
 "data": [
  {
     "dateCreated": "02-04-2016",
     "billable": true,
     "minutes": 150,
     "employeeId": {
        "id": 102522
        }
  },
  {
     "dateCreated": "03-04-2016",
     "billable": true,
     "minutes": 250,
     "employeeId": {
        "id": 102522
   }
  },
  {
     "dateCreated": "04-04-2016",
     "billable": true,
     "minutes": 20,
     "employeeId": {
        "id": 135522
   }
  }
]
}
正如您所看到的,来自集合的employeeDetails.id与来自集合的条目的employeeId.id匹配

我正在尝试进行查找,以便将条目中的id与Employees中的用户名进行匹配,以便将名称与条目进行匹配,所需结果如下:

{
 "data": [
  {
     "full name": "Keith Richards",
     "dateCreated": "02-04-2016",
     "minutes": 150,
     "employeeId": {
        "id": 102522
        }
  },
  {
     "full name": "Keith Richards",
     "dateCreated": "03-04-2016",
     "minutes": 250,
     "employeeId": {
        "id": 102522
        }
  },
  {
     "full name": "Jim Morrison",
     "dateCreated": "04-04-2016",
     "minutes": 20,
     "employeeId": {
        "id": 135522
  }
 }
]
}
这将允许我获得组/总和的最终结果:

  • 全名:基思·理查兹
  • 分钟:400
  • 全名:吉姆·莫里森
  • 分钟:20
我已经尝试了很多,唯一接近成功的方法是:

db.getCollection('entries')
.aggregate(
  [
    {
      "$lookup": {
        "from": "employees", 
        "localField": "trafficEmployeeId.id", 
        "foreignField": "employeeDetails.id", 
        "as": "employees_loaded"
      }
    }
  ]
);
这基本上给了我一个包含两个不同数组的集合


有人能给我建议/解决方案,告诉我实现目标的最佳方法吗?我选择了MongoDB,因为输入严重依赖于JSON输入。

您需要将
数据
数组本身的内容作为收集项。例如,您的
员工
集合应如下所示:

{
    "full name": "Keith Richards",
    "age": 21,
    "userName": "keith1@keith.com",
    "employeeDetails": {
        "id": 102522
    }
}, {
    "full name": "Jim Morrison",
    "age": 27,
    "userName": "jim@jim.com",
    "employeeDetails": {
        "id": 135522
    }
}
这样您就可以像这样轻松地
$lookup

db.entries.aggregate(
    [
        { $unwind: "$data" }, {
            $lookup: {
                "from": "employees",
                "localField": "data.employeeId.id",
                "foreignField": "employeeDetails.id",
                "as": "employees_loaded"
            }
        }, {
            $unwind: "$employees_loaded"
        }, {
            $group: {
                "_id": 1,
                "data": {
                    $push: {
                        "full name": "$employees_loaded.full name",
                        "dateCreated": "$data.dateCreated",
                        "minutes": "$data.minutes",
                        "employeeId": "$data.employeeId"
                    }
                }
            }
        }
    ]
);

$unwind
用于删除从
$lookup
生成的
employees\u loaded
数组。
$group
阶段用于获取您想要保留的字段,并
$push
将它们放入一个名为
数据

的数组中。非常感谢这让我更亲近了。问题是,它给了我4个数组,全名,dateCreated,minutes和employeeId。它们只是列出的,但不匹配。我是否需要将上述内容包装为“匹配”或函数?链接到此处的示例:是否要匹配特定的用户名或employeeId?两个集合都具有employeeId,但只有Employee集合具有用户名。我希望能够在Entries集合中将Employee的用户名与employeeId相匹配。通过这种方式,我可以输出一个entires(分钟)列表,并让每个entires显示用户名。在entries集合中,您是否像employee集合中一样将数据数组项移动到集合本身?不,根据文章顶部的JSON示例,它们位于单独的集合中。