gpt4 book ai didi

mongodb - 如何在 MongoDB 聚合中展开多个数组

转载 作者:可可西里 更新时间:2023-11-01 09:34:43 24 4
gpt4 key购买 nike

这是我的文档的样子

{
"_id" : ObjectId("584149cafda90a8b18cdfcc1"),
"uid" : "583eaa7df4def0ec5a520d19",
"surid" : "58414631ec5ed099538929b8",
"createdat" : ISODate("2016-12-02T10:15:38.382Z"),
"response" : [
{
"qid" : "649975800",
"que" : "Which is your favourite color ?",
"ans" : [
"red",
"yellow"
]
},
{
"qid" : "309541969",
"que" : "which is your favourite fruits ? ",
"ans" : [
"apple",
"orange"
]
}
]
}

/* 2 */
{
"_id" : ObjectId("58414a28fda90a8b18cdfcc7"),
"uid" : "57ff2141b893ba1a2e89ef57",
"surid" : "58414631ec5ed099538929b8",
"createdat" : ISODate("2016-12-02T10:17:12.800Z"),
"response" : [
{
"qid" : "649975800",
"que" : "Which is your favourite color ?",
"ans" : "red"
},
{
"qid" : "309541969",
"que" : "which is your favourite fruits ? ",
"ans" : "banana"
}
]
}

/* 3 */
{
"_id" : ObjectId("58414a52fda90a8b18cdfcd1"),
"uid" : "57b300678c9f14d7555b668e",
"surid" : "58414631ec5ed099538929b8",
"createdat" : ISODate("2016-12-02T10:17:54.869Z"),
"response" : [
{
"qid" : "649975800",
"que" : "Which is your favourite color ?",
"ans" : "red"
},
{
"qid" : "309541969",
"que" : "which is your favourite fruits ? ",
"ans" : "banana"
}
]
}

这是我需要的:

{
"que" : "Which is your favourite color ?",
"ans" :{red:3, yellow:1}
},
{
"que" : "which is your favourite fruits ? ",
"ans":{apple:1, orange:1, banana:3}
}

我想使用唯一的 surid 和单独的答案通过 mongodb 聚合得到这个结果。一切以用户数据的反馈结果为准。

最佳答案

因为您不会事先知道嵌入的 ans 数组的值,所以建议的期望输出将不可行,因为它假定您知道这些值。一种更好更快的方法是将输出作为嵌入式计数文档获取,例如:

{
"ques": "Which is your favourite color ?",
"counts": [
{ "value": "red", "count": 3 },
{ "value": "yellow", "count": 1 }
]
},
{
"ques": "which is your favourite fruits ?",
"counts": [
{ "value": "apple", "count": 1 },
{ "value": "orange", "count": 1 },
{ "value": "banana", "count": 3 }
]
}

这可以通过运行这个聚合操作来实现:

db.collection.aggregate([
{ "$unwind": "$response" },
{ "$unwind": "$response.ans" },
{
"$group": {
"_id": {
"surid": "$surid",
"ans": "$response.ans"
},
"ques": { "$first": "$reponse.que" },
"count": { "$sum": 1 }
}
},
{
"$group": {
"_id": "$_id.surid",
"ques": { "$first": "$ques" },
"counts": {
"$push": {
"value": "$_id.ans",
"count": "$count"
}
}
}
}
])

但是,如果这些值是静态的并且事先已知,则可以利用 $cond 运算符在 $group 阶段根据 "response.ans" 字段评估计数,如下所示:

db.collection.aggregate([
{ "$unwind": "$response" },
{ "$unwind": "$response.ans" },
{
"$group": {
"_id": "$surid",
"ques": { "$first": "$reponse.que" },
"red": {
"$sum": {
"$cond": [ { "$eq": [ "$response.ans", "red" ] }, 1, 0 ]
}
},
"yellow": {
"$sum": {
"$cond": [ { "$eq": [ "$response.ans", "yellow" ] }, 1, 0 ]
}
},
"apple": {
"$sum": {
"$cond": [ { "$eq": [ "$response.ans", "apple" ] }, 1, 0 ]
}
},
"orange": {
"$sum": {
"$cond": [ { "$eq": [ "$response.ans", "orange" ] }, 1, 0 ]
}
},
"banana": {
"$sum": {
"$cond": [ { "$eq": [ "$response.ans", "banana" ] }, 1, 0 ]
}
}
}
}
])

关于mongodb - 如何在 MongoDB 聚合中展开多个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40932518/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com