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

Mongodb 我应该选择mongo聚合还是应该在应用程序级服务上选择它,mongodb,aggregation-framework,Mongodb,Aggregation Framework,我有以下收藏 公司: 用户: 主题类别 话题 尺寸 询问 用户响应 上面发生了什么 每个用户都属于一个公司 每个主题都属于一个主题类别 每个查询都属于一个主题 每个查询都有一个维度 用户可以回答有关评级的查询 用户对每个问题的回答将存储在用户回答中 我想要达到的目标 基于主题类别和维度 预期聚合结果格式 查询基于一个主题类别 每个主题的所有问题都根据主题进行汇总 每个主题都会根据维度计算出平均评分 回应 我可以使用Mongo聚合实现这一点,还是应该在服务级别实现这一点? 我试过做什么 我得到了什

我有以下收藏

公司:

用户:

主题类别

话题

尺寸

询问

用户响应

上面发生了什么

  • 每个用户都属于一个公司
  • 每个主题都属于一个主题类别
  • 每个查询都属于一个主题
  • 每个查询都有一个维度
  • 用户可以回答有关评级的查询
  • 用户对每个问题的回答将存储在用户回答中
  • 我想要达到的目标

    基于主题类别维度

    预期聚合结果格式

  • 查询基于一个主题类别
  • 每个主题的所有问题都根据主题进行汇总
  • 每个主题都会根据维度计算出平均评分 回应

    我可以使用Mongo聚合实现这一点,还是应该在服务级别实现这一点?

    我试过做什么

    我得到了什么

    还需要什么

    • 每个回答都有一个问题。每个问题都有一个维度
    • 我需要得到基于维度的平均评分

    谢谢。

    您想将
    主题类别
    维度
    ID作为输入传递吗?如果是这样,在哪里可以使用
    公司
    集合?我认为您应该只使用聚合,由于投影的字段有限。@styopdev
    Topic category id
    是一个输入,但需要从每个回答中的问题中提取
    Dimension
    。@saikatchakrabortty我能够根据主题对结果进行分组,并且能够以数组的形式获取与主题相关的回答。现在,我如何根据回答中问题的维度得出平均值。我在这里很惊讶。也许你应该使用$group运算符,如果你将当前查询添加到问题中,会更清楚。
    {
    "_id" : ObjectId("5a7848e8ca70273218e9d743"),
    "name" : "Google",
    "departments" : [ 
        {
            "name" : "IT",
            "_id" : "1234567890"
        }, 
        {
            "name" : "Sales",
            "_id" : "1234567891"
        }
    ]
    }
    
    {
        "_id" : ObjectId("5a784977ca70273218e9d759"),
        "name" : "Sankarshan",
        "company" : ObjectId("5a7848e8ca70273218e9d743"),
        "department" : "1234567890"
    }
    
    {
    "_id" : ObjectId("5a784a10ca70273218e9d76f"),
    "name" : "topicCtegoryOne",
    "order" : 1
    }
    
    {
    "_id" : ObjectId("5a784cc3ca70273218e9d7d3"),
    "topicCategory" : ObjectId("5a784a10ca70273218e9d76f"),
    "name" : "TopicOne",
    "order" : 1,
    "label" : "oneTopic",
    "color" : "red"
    }
    
    {
    "_id" : ObjectId("5a784fdcca70273218e9d869"),
    "name" : "dimentionOne"
    }
    
    {
    "_id" : ObjectId("5a78519aca70273218e9d8d7"),
    "topic" : ObjectId("5a784cc3ca70273218e9d7d3"),
    "dimention" : ObjectId("5a784fdcca70273218e9d869"),
    "order" : 1,
    "label" : "queryLabelOne",
    "statement" : "This is one question - Top1"
    }
    
    {
    "_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"
    }
    }
    
    {
    "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
                }
            ]
        }
    
    ]   
    }
    
    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
    }
    }