使用Mongo聚合框架的共现计数 [英] Co-ocurrence count using Mongo aggregation framework
问题描述
我拥有与客户互动的产品(无重复产品)的文档:
I have this documents with the products that the client interacted with (no repeated products):
{ "client_interactions": [{"productType": "A", "productId": "1"}, {"productType": "A", "productId": "2"}, {"productType": "B", "productId": "9"}]}
{ "client_interactions": [{"productType": "A", "productId": "1"}, {"productType": "A", "productId": "2"}]}
{ "client_interactions": [{"productType": "A", "productId": "1"}, {"productType": "A", "productId": "3"}, {"productType": "C", "productId": "10"}]}
我想如果客户已将产品 1与产品 2同时使用,则计算 A类型的每种产品的同时发生次数。
I want to calculate the count of co-ocurrences for each product of type "A", in the sense that product "1" co-ocurred with product "2" if a client has interacted with both of them.
类似的东西:
{ "co-ocurrences-count" : { "1" : [{ "2": 2}, { "3" : 1}]}, { "2" : [{ "1": 2}]}, { "3" : [{ "1": 1}]}}
我有一个使用map-reduce javascript fu的解决方案功能,但我真的希望它使用MongoDB聚合框架来实现吗?
I have a solution using map-reduce javascript functions, but I really want it to do using the MongoDB aggregation framework, is it possible?
预先感谢。
推荐答案
聚合时间很长,但是有效。这个想法是,您需要基于 client_interactions
数组构建对(x,y)
。可以使用 $ reduce 和 $ map 。然后,您需要运行 $ unwind 和几个 $ group 阶段来汇总您的汇总数据。您还需要 $ arrayToObject 才能动态构建密钥。
The Aggregation is pretty long but it works. The idea is that you need to build pairs (x,y)
based on your client_interactions
array. It can be done using $reduce and $map. Then you need to run $unwind and a couple of $group stages to "wind" your aggregated data. You also need $arrayToObject to build your keys dynamically.
db.collection.aggregate([
{
$addFields: {
"client_interactions": {
$filter: { input: "$client_interactions", cond: { $eq: [ "$$this.productType", "A" ] } }
}
}
},
{
$project: {
a: {
$reduce: {
input: "$client_interactions",
initialValue: [],
in: {
$concatArrays: [
"$$value",
{ $map: { input: "$client_interactions", as: "c", in: { x: "$$this.productId", y: "$$c.productId" } } }
]
}
}
}
}
},
{
$unwind: "$a"
},
{
$match: {
$expr: {
$ne: [ "$a.x", "$a.y" ]
}
}
},
{
$sort: {
"a.x": 1,
"a.y": 1
}
},
{
$group: {
_id: "$a",
count: { $sum: 1 }
}
},
{
$group: {
_id: "$_id.x",
arr: { $push: { k: "$_id.y", v: "$count" } }
}
},
{
$group: {
_id: null,
"co-ocurrences-count": { $push: { k: "$_id", v: { $arrayToObject: "$arr" } } }
}
},
{
$project: {
_id: 0,
"co-ocurrences-count": { $arrayToObject: "$co-ocurrences-count" }
}
}
])
这篇关于使用Mongo聚合框架的共现计数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!