我有一个名为Vote的集合,如下所示:

{
  postId: "1",
  comment:{ 
    text_sentiment: "positive",
    topic: "A"
  }
}, // DOC-1

{
  postId: "2",
  comment:{ 
     text_sentiment: "negative",
     topic: "A"
  }
}, // DOC-2

{
  postId: "3",
  comment:{ 
     text_sentiment: "positive",
     topic: "B"
  }
},..//DOC-3 .. 

还有一个名为post的集合,如下所示?

{
  _id: "1",
  category: "a"
},
{
  _id: "2",
  category: "b",
}

我想在此集合上进行聚合,以便它返回以下 struct .(其中还考虑了其他post个集合`

[
   {
      _id: "hash",
      topic: "A",
      topicOccurance: 2,
      sentiment: {
        positive: 1,
        negative: 1,
        neutral: 0
      },
      postIds: [1,2],
      categories: [a,b]
   },
   ..
]

我创建了以下聚合:

    db.Vote.aggregate([
    {
        $match: {
            surveyId: "e6d38e1ecd",
            "comment.topic": {
                $exists: 1
            },

        }
    },
    {
        $group: {
            _id: {
                topic: "$comment.topic",
                text_sentiment: "$comment.text_sentiment"
            },
            total: {
                $sum: 1
            },
            postIds: {
                $push: "$postId"
            }

        }
    },
    {
        $group: {
            _id: "$_id.topic",
            total: {
                $sum: "$total"
            },
            text_sentiments: {
                $push: {
                    k: "$_id.text_sentiment",
                    v: "$total"
                }
            },
            postIds: {
                "$push": "$postIds"
            }
        }
    },
    {
        $project: {
            topic: "$_id",
            topicOccurance: "$total",
            sentiment: {
                "$arrayToObject": "$text_sentiments"
            },
            postIds: {
                $reduce: {
                  input: "$postIds",
                  initialValue: [],
                  in: {
                    $concatArrays: [
                      "$$value",
                      "$$this"
                    ]
                  }
                }
            }
        }
    },
    {
        $sort: {
            "topicOccurance": -1
        }
    }
])

这很好,但我不知道如何也得到categories个存在于名为posts的单独集合中.如何在执行聚合时查询其他集合?

推荐答案

一个简单的$lookup就足够了:

db.Vote.aggregate([
  {
    $match: {
      "comment.topic": {
        $exists: 1
      },
      
    }
  },
  {
    $group: {
      _id: {
        topic: "$comment.topic",
        text_sentiment: "$comment.text_sentiment"
      },
      total: {
        $sum: 1
      },
      postIds: {
        $push: "$postId"
      }
    }
  },
  {
    $group: {
      _id: "$_id.topic",
      total: {
        $sum: "$total"
      },
      text_sentiments: {
        $push: {
          k: "$_id.text_sentiment",
          v: "$total"
        }
      },
      postIds: {
        "$push": "$postIds"
      }
    }
  },
  {
    $project: {
      topic: "$_id",
      topicOccurance: "$total",
      sentiment: {
        "$arrayToObject": "$text_sentiments"
      },
      postIds: {
        $reduce: {
          input: "$postIds",
          initialValue: [],
          in: {
            $concatArrays: [
              "$$value",
              "$$this"
            ]
          }
        }
      }
    }
  },
  {
    $sort: {
      "topicOccurance": -1
    }
  },
  {
    $lookup: {
      from: "post",
      localField: "postIds",
      foreignField: "_id",
      as: "categories"
    }
  },
  {
    $addFields: {
      categories: {
        "$setUnion": {
          $map: {
            input: "$categories",
            in: "$$this.category"
          }
        }
      }
    }
  }
])

Mongo Playground

Javascript相关问答推荐

如何判断属于多个元素的属性是否具有多个值之一

materialized - activeIndex返回-1

在React中获取数据后,如何避免不必要的组件闪现1秒?

如何在Angular中插入动态组件

jQuery s data()[storage object]在vanilla JavaScript中?'

如何调用名称在字符串中的实例方法?

类构造函数忽略Reaction Native中的可选字段,但在浏览器中按预期工作

以编程方式聚焦的链接将被聚焦,但样式不适用

使用插件构建包含chart.js提供程序的Angular 库?

<;img>;标记无法呈现图像

在D3条形图中对具有相同X值的多条记录进行分组

如何在脚本编译后直接将RxJ模块导入浏览器(无需Angel、webpack、LiteServer)

无法使用npm install安装react-dom、react和next

Django模板中未加载JavaScript函数

如果查询为空,则MongoDB将所有文档与$in匹配

react :图表负片区域不同 colored颜色

单击子按钮时将活动切换类添加到父分区

如何根据获取的对象数量动态生成状态变量

查找函数句柄的模块/文件

App Script如何读取通过云函数传递的表单对象