gpt4 book ai didi

javascript - 在 MapReduce MongoDB 中按键对数据进行分组

转载 作者:可可西里 更新时间:2023-11-01 10:21:32 25 4
gpt4 key购买 nike

我正在尝试 MongoDB 中的 MapReduce 程序来寻找共同的 friend ,我在对 mongoDB 中的键进行排序后获得了以下数据

{"user" : " Hari","friend" : "Shiva",
"friendList": ["Hanks"," Tom"," Karma"," Hari"," Dinesh"]}


{"user" : "Hari","friend" : " Shiva",
"friendList" : ["Karma"," Tom"," Ram"," Bindu"," Shiva",
" Kishna"," Bikash"," Bakshi"," Dinesh"]}

现在我想将这些具有相同键的数据集分组到一个组中,在将键值对发送到 reducer 之前在 map 函数中使用 Javascript,我如何对数据进行分组?例如我想要这样的输出

{"user" : " Hari","friend" : "Shiva",
"friendList": ["Hanks"," Tom"," Karma"," Hari"," Dinesh"],["Karma"," Tom"," Ram"," Bindu"," Shiva"," Kishna"," Bikash"," Bakshi"," Dinesh"]}

最佳答案

您可以将两条记录的 friendlist 数组连接成一个数组,以创建如下对象:

   {
"_id": {
"user": " Hari",
"friend": "Shiva"
},
"value": {
"friendList": [
"Hanks",
" Tom",
" Karma",
" Hari",
" Dinesh",
"Karma",
" Tom",
" Ram",
" Bindu",
" Shiva",
" Kishna",
" Bikash",
" Bakshi",
" Dinesh"
]
}
}

请参阅 https://jsfiddle.net/b6hxswvk/1/ 处的代码创建这个单个对象

如果您希望好友列表 是一个二维数组,即像这样:

{
"_id": {
"user": " Hari",
"friend": "Shiva"
},
"value": {
"friendList": [
[
"Hanks",
" Tom",
" Karma",
" Hari",
" Dinesh"
],
[
"Karma",
" Tom",
" Ram",
" Bindu",
" Shiva",
" Kishna",
" Bikash",
" Bakshi",
" Dinesh"
]
]
}
}

您可以使用 https://jsfiddle.net/b6hxswvk/2/ 处的代码

关于javascript - 在 MapReduce MongoDB 中按键对数据进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38966432/

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