如何在mongodb中使用group by?

时间:2019-05-09 04:40:48

标签: mongodb mongoose mongodb-query aggregation-framework

我想用逗号分隔值。那么如何在聚合框架中使用group / concat?

文档联系人

\1

文档机会

{
  "_id" : "9c612c95054fb46e3de8dee8",
  "name" : "name1",
  "oppID" : [
        "5c612c95054fb46e3de8bcc5",
        "5bd6b334cba7d2241a3ba9d9"
       ]
},
{
  "_id" : "1c612c95054fb46e3de8dcde9",
  "name" : "name2",
  "oppID" : [
        "5c612c95054fb46e3de8bcc5",
        "5bd6b334cba7d2241a3ba9d9"
       ]
}

我想获得如下的联系人列表,

{
  "_id" : "5c612c95054fb46e3de8bcc5",
  "name" : "opp name 01",
},
{
  "_id" : "5bd6b334cba7d2241a3ba9d9",
  "name" : "opp name 02",
},

1 个答案:

答案 0 :(得分:1)

您需要运行$lookup从两个集合中获取数据,然后使用$reduce$concat将数组转换为单个字符串

{{1}}