我有以下收藏品。
公司:
{
"_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"
}
}
上面发生了什么
我想要实现的目标
基于主题类别和维度
的回复汇总结果预期的汇总结果格式
响应
{
"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
}
}
还需要更多
谢谢。