猫鼬:聚合和计数
Mongoose: Aggregating and Counting
本文关键字:猫鼬 更新时间:2023-09-26
我有一个NodeJS/MongoDB应用程序,它的数据存储在一个名为"Feedback"
的集合中。数据如下所示:
[
{
"__v": 0,
"_id": "57d6b2d09f46ca14440ac14e",
"customerFeedback": [
{
"_id": "57d6b2d09f46ca14440ac14f",
"answerswer": [
{
"_id": "57d6b2d09f46ca14440ac150",
"answerswerValue": "cat",
"answerswerWeight": 0
}
],
"question": "What is your favourite thing about this shop?",
"questionId": "57d65edc0132461120fa0afd"
},
{
"_id": "57d6b2d09f46ca14440ac151",
"answerswer": [
{
"_id": "57d6b2d09f46ca14440ac152",
"answerswerValue": "Okay",
"answerswerWeight": 0
}
],
"question": "How was your experience today?",
"questionId": "57d69ef6dbb25611e46e6bc9"
}
],
"shopId": "SH0001",
"feedbackCreatedOn": "2016-09-12T13:51:12.703Z",
"questionsForDay": "2016-09-12T00:00:00Z"
},
{
"__v": 0,
"_id": "57d6b3389f46ca14440ac157",
"customerFeedback": [
{
"_id": "57d6b3389f46ca14440ac158",
"answerswer": [
{
"_id": "57d6b3389f46ca14440ac159",
"answerswerValue": "cat",
"answerswerWeight": 0
}
],
"question": "What is your favourite thing about this shop?",
"questionId": "57d65edc0132461120fa0afd"
},
{
"_id": "57d6b3389f46ca14440ac15a",
"answerswer": [
{
"_id": "57d6b3389f46ca14440ac15b",
"answerswerValue": "Very Good",
"answerswerWeight": 0
}
],
"question": "How was your experience today?",
"questionId": "57d69ef6dbb25611e46e6bc9"
},
{
"_id": "57d6b3389f46ca14440ac15c",
"answerswer": [
{
"_id": "57d6b3389f46ca14440ac15d",
"answerswerValue": "Cost",
"answerswerWeight": 0
}
],
"question": "What would you like us to improve on?",
"questionId": "57d6b32d9f46ca14440ac153"
}
],
"shopId": "SH0001",
"feedbackCreatedOn": "2016-09-12T13:52:56.939Z",
"questionsForDay": "2016-09-12T00:00:00Z"
},
{
"__v": 0,
"_id": "57d6c8eb97157f10a4e5c2e7",
"customerFeedback": [
{
"_id": "57d6c8eb97157f10a4e5c2e8",
"answerswer": [
{
"_id": "57d6c8eb97157f10a4e5c2ea",
"answerswerValue": "Customer Experience",
"answerswerWeight": 0
},
{
"_id": "57d6c8eb97157f10a4e5c2e9",
"answerswerValue": "Others",
"answerswerWeight": 0
}
],
"question": "What would you like us to improve on?",
"questionId": "57d6b7d99ee61e47f01e5334"
}
],
"shopId": "SH0003",
"feedbackCreatedOn": "2016-09-12T15:25:31.724Z",
"questionsForDay": "2016-09-12T00:00:00Z"
}
]
在结果数组中有许多这样的条目,但上面的数据可以说明这一点。
我的问题是,对于给定的shopId
和questionId
,我想计算每个单独的answerValue
出现的次数。我该怎么做呢?
我能够使用find方法将结果过滤到所需的数据集,如(例如)
db.Feedback.find({shopId:"SH0001",'customerFeedback.questionId':"57d65edc0132461120fa0afd"})
但是我不确定如何将数据聚合到我想要的格式。
这个管道应该会给你想要的结果
db.getCollection("yourCollection").aggregate([
{ $unwind: "$customerFeedback" },
{ $unwind: "$customerFeedback.answer" },
{
$group: {
_id: {
shopId: "$shopId",
questionId: "$customerFeedback.questionId",
answerValue: "$customerFeedback.answer.answerValue"
},
count: { $sum: 1 }
}
}
])
对于您的示例数据,它给出以下输出
/* 1 */
{
"_id" : {
"shopId" : "SH0003",
"questionId" : "57d6b7d99ee61e47f01e5334",
"answerswerValue" : "Others"
},
"count" : 1.0
}
/* 2 */
{
"_id" : {
"shopId" : "SH0003",
"questionId" : "57d6b7d99ee61e47f01e5334",
"answerswerValue" : "Customer Experience"
},
"count" : 1.0
}
/* 3 */
{
"_id" : {
"shopId" : "SH0001",
"questionId" : "57d65edc0132461120fa0afd",
"answerswerValue" : "cat"
},
"count" : 2.0
}
/* 4 */
{
"_id" : {
"shopId" : "SH0001",
"questionId" : "57d69ef6dbb25611e46e6bc9",
"answerswerValue" : "Okay"
},
"count" : 1.0
}
/* 5 */
{
"_id" : {
"shopId" : "SH0001",
"questionId" : "57d6b32d9f46ca14440ac153",
"answerswerValue" : "Cost"
},
"count" : 1.0
}
/* 6 */
{
"_id" : {
"shopId" : "SH0001",
"questionId" : "57d69ef6dbb25611e46e6bc9",
"answerswerValue" : "Very Good"
},
"count" : 1.0
}
当然,如果您只对特定值感兴趣,您可以使用额外的$match阶段过滤结果
由于注释而更新:
要用$match
过滤你的结果,你可以在展开嵌套的客户反馈数组后使用它,像这样
...
{ $unwind: "$customerFeedback" },
{ $match: { shopId: "SH0001", "customerFeedback.questionId": "57d65edc0132461120fa0afd" } },
{ $unwind: "$customerFeedback.answer" },
{
$group: {
_id: {
shopId: "$shopId",
questionId: "$customerFeedback.questionId",
answerValue: "$customerFeedback.answer.answerValue"
},
count: { $sum: 1 }
}
}
...
会导致
{
"_id" : {
"shopId" : "SH0001",
"questionId" : "57d65edc0132461120fa0afd",
"answerswerValue" : "cat"
},
"count" : 2.0
}
如果您在shopId
和/或customerFeedback.questionId
上有大量文档或索引,那么您可能希望将$match
阶段复制到管道的前面,以便您只展开相应商店的文档,并且至少有一个所需问题的反馈。这样(从正确性的角度来看是可选的优化),它看起来像这样
...
{ $match: { shopId: "SH0001", "customerFeedback.questionId": "57d65edc0132461120fa0afd" } },
{ $unwind: "$customerFeedback" },
{ $match: { shopId: "SH0001", "customerFeedback.questionId": "57d65edc0132461120fa0afd" } },
{ $unwind: "$customerFeedback.answer" },
{
$group: {
_id: {
shopId: "$shopId",
questionId: "$customerFeedback.questionId",
answerValue: "$customerFeedback.answer.answerValue"
},
count: { $sum: 1 }
}
}
...
相关文章:
- 猫鼬在特定记录中查找嵌套记录
- 猫鼬——如何找到物体所在的场&;name”;不包含数字
- 将图像文件存储在猫鼬模式中的二进制数据中,并以html形式显示图像
- 请求对象中的MEANJS猫鼬模型
- 哈希被推到猫鼬模型上作为'[对象对象]”;
- 猫鼬:$where不等
- 节点.js猫鼬性能
- 在 Node.js 中填充来自猫鼬的对象
- 没有字段名称的猫鼬结构聚合输出
- 使用猫鼬更新子文档
- 无法传递猫鼬数据来查看翡翠
- 在猫鼬中查找自定义条件
- 如何在猫鼬中异步填充嵌套对象
- 是否可以引用猫鼬(MongoDB)上的子文档
- 在这个猫鼬独特的保存前验证中,我做错了什么
- 猫鼬承诺和Q承诺
- 在模式方法内部,“this”在猫鼬 4.4.12 中为空 {}
- 蓝鸟与猫鼬,类型错误:.create(..).然后(..).Nodeify不是一个函数
- 猫鼬找到一个并推送到一排文档
- 猫鼬连接在 Express.js 应用程序中自动共享