我应该选择mongo聚合还是应该在应用程序级服务

时间:2018-02-06 06:13:47

标签: mongodb aggregation-framework

我有以下收藏品。

  

公司:

{
"_id" : ObjectId("5a7848e8ca70273218e9d743"),
"name" : "Google",
"departments" : [ 
    {
        "name" : "IT",
        "_id" : "1234567890"
    }, 
    {
        "name" : "Sales",
        "_id" : "1234567891"
    }
]
}
  

用户:

{
    "_id" : ObjectId("5a784977ca70273218e9d759"),
    "name" : "Sankarshan",
    "company" : ObjectId("5a7848e8ca70273218e9d743"),
    "department" : "1234567890"
}
  

topicCategories

{
"_id" : ObjectId("5a784a10ca70273218e9d76f"),
"name" : "topicCtegoryOne",
"order" : 1
}
  

主题

{
"_id" : ObjectId("5a784cc3ca70273218e9d7d3"),
"topicCategory" : ObjectId("5a784a10ca70273218e9d76f"),
"name" : "TopicOne",
"order" : 1,
"label" : "oneTopic",
"color" : "red"
}
  

dimentions

{
"_id" : ObjectId("5a784fdcca70273218e9d869"),
"name" : "dimentionOne"
}
  

查询

{
"_id" : ObjectId("5a78519aca70273218e9d8d7"),
"topic" : ObjectId("5a784cc3ca70273218e9d7d3"),
"dimention" : ObjectId("5a784fdcca70273218e9d869"),
"order" : 1,
"label" : "queryLabelOne",
"statement" : "This is one question - Top1"
}
  

USER_RESPONSES

{
"_id" : ObjectId("5a7859f5ca70273218e9da7d"),
"user" : ObjectId("5a784977ca70273218e9d759"),
"company" : ObjectId("5a7848e8ca70273218e9d743"),
"department" : "1234567890",
"response" : {
    "question" : ObjectId("5a78519aca70273218e9d8d7"),
    "topic" : ObjectId("5a784cc3ca70273218e9d7d3"),
    "topicCategory" : ObjectId("5a784a10ca70273218e9d76f"),
    "dimention" : ObjectId("5a784fdcca70273218e9d869"),
    "rating" : 5,
    "color" : "red"
}
}
  

上面发生了什么

  1. 每个用户都属于公司。
  2. 每个主题都属于主题类别。
  3. 每个查询属于主题
  4. 每个查询都有一个维度。
  5. 用户可以在评分方面回答查询。
  6. 用户对每个问题的回复将存储在User_responses中。
  7.   

    我想要实现的目标

    基于主题类别维度

    的回复汇总结果
      

    预期的汇总结果格式

    1. 查询基于一个主题类别。
    2. 每个主题的所有问题都是根据主题
    3. 汇总的
    4. 每个主题都会根据维度计算平均评分。
    5.   

      响应

      {
      "topicCategory": "topic category",
      "topics": [
          {
              "topicLabel": "label of the topic one",
              "dimentions": [
                  {
                      "name": "dimention name one",
                      "avgValue": 5.6
                  }
                  {
                      "name": "dimention name two",
                      "avgValue": 4.5
                  }
              ]
          },
          {
              "topicLabel": "label of the topic two",
              "dimentions": [
                  {
                      "name": "dimention name one",
                      "avgValue": 6.6
                  }
                  {
                      "name": "dimention name two",
                      "avgValue": 9.5
                  }
              ]
          }
      
      ]   
      }
      

      我可以使用Mongo聚合实现此功能,还是应该在服务级别执行此操作?

        

      我尝试过做什么

      db.getCollection('collResp').aggregate([
      {
          $match: 
          { 
              company: ObjectId("5a7848e8ca70273218e9d743"),
              department: "1234567890"
          }
      },
      {
          $group: {
              _id: {
                  topic: "$response.topic",
                  category: "$response.topicCategory"
              },
              responses: {
                  $push: "$response"
              }
          }
      },
      {
          $lookup: {
              from: 'topics',
              localField: '_id.topic',
              foreignField: '_id',
              as: "topic"
          }
      },
      {
          $unwind: {
              path: "$topic"
          }
      },
      {
          $lookup: {
              from: 'topicCategories',
              localField: '_id.category',
              foreignField: '_id',
              as: "category"
          }
      },
      {
          $unwind: {
              path: "$category"
          }
      }
      ])
      
        

      我得到了什么

      /* 1 */
      {
      "_id" : {
          "topic" : ObjectId("5a78512cca70273218e9d8bd"),
          "category" : ObjectId("5a784a10ca70273218e9d76f")
      },
      "responses" : [ 
          {
              "question" : ObjectId("5a7851a6ca70273218e9d8d9"),
              "topic" : ObjectId("5a78512cca70273218e9d8bd"),
              "topicCategory" : ObjectId("5a784a10ca70273218e9d76f"),
              "dimention" : ObjectId("5a784fdcca70273218e9d869"),
              "rating" : 8,
              "color" : "red"
          }, 
          {
              "question" : ObjectId("5a78580eca70273218e9da10"),
              "topic" : ObjectId("5a78512cca70273218e9d8bd"),
              "topicCategory" : ObjectId("5a784a10ca70273218e9d76f"),
              "dimention" : ObjectId("5a7851d5ca70273218e9d8ee"),
              "rating" : 7,
              "color" : "red"
          }
      ],
      "topic" : {
          "_id" : ObjectId("5a78512cca70273218e9d8bd"),
          "topicCategory" : ObjectId("5a784a10ca70273218e9d76f"),
          "name" : "TopicTwo",
          "order" : 2,
          "label" : "twoTopic",
          "color" : "green"
      },
      "category" : {
          "_id" : ObjectId("5a784a10ca70273218e9d76f"),
          "name" : "topicCtegoryOne",
          "order" : 1
      }
      }
      
      /* 2 */
      {
      "_id" : {
          "topic" : ObjectId("5a784cc3ca70273218e9d7d3"),
          "category" : ObjectId("5a784a10ca70273218e9d76f")
      },
      "responses" : [ 
          {
              "question" : ObjectId("5a78519aca70273218e9d8d7"),
              "topic" : ObjectId("5a784cc3ca70273218e9d7d3"),
              "topicCategory" : ObjectId("5a784a10ca70273218e9d76f"),
              "dimention" : ObjectId("5a784fdcca70273218e9d869"),
              "rating" : 5,
              "color" : "red"
          }, 
          {
              "question" : ObjectId("5a785807ca70273218e9da0e"),
              "topic" : ObjectId("5a784cc3ca70273218e9d7d3"),
              "topicCategory" : ObjectId("5a784a10ca70273218e9d76f"),
              "dimention" : ObjectId("5a7851d5ca70273218e9d8ee"),
              "rating" : 3,
              "color" : "red"
          }
      ],
      "topic" : {
          "_id" : ObjectId("5a784cc3ca70273218e9d7d3"),
          "topicCategory" : ObjectId("5a784a10ca70273218e9d76f"),
          "name" : "TopicOne",
          "order" : 1,
          "label" : "oneTopic",
          "color" : "red"
      },
      "category" : {
          "_id" : ObjectId("5a784a10ca70273218e9d76f"),
          "name" : "topicCtegoryOne",
          "order" : 1
      }
      }
      
        

      还需要更多

      • 每个回复都有一个问题。每个问题都有一个维度
      • 我需要根据维度获得平均评分。

      谢谢。

0 个答案:

没有答案