获取MongoDB聚合框架中数组交集的大小
我目前正在Java web应用程序中使用MongoDB的聚合框架,根据其他用户的偏好为用户生成建议 我使用的主要方法之一是查看数组的交集 现在,我的算法只考虑两个用户相似,如果他们有一个非零数组交集 为了构建更精确的算法,我想在聚合管道中权衡集合交集的大小获取MongoDB聚合框架中数组交集的大小,mongodb,mongodb-java,aggregation-framework,Mongodb,Mongodb Java,Aggregation Framework,我目前正在Java web应用程序中使用MongoDB的聚合框架,根据其他用户的偏好为用户生成建议 我使用的主要方法之一是查看数组的交集 现在,我的算法只考虑两个用户相似,如果他们有一个非零数组交集 为了构建更精确的算法,我想在聚合管道中权衡集合交集的大小 有办法做到这一点吗?如果我理解你的问题,你有如下数据: db.users.insert({_id: 100, likes: [ 'pina coladas', 'long walks on the beach', 'g
有办法做到这一点吗?如果我理解你的问题,你有如下数据:
db.users.insert({_id: 100, likes: [
'pina coladas',
'long walks on the beach',
'getting caught in the rain'
]})
db.users.insert({_id: 101, likes: [
'cheese',
'bowling',
'pina coladas'
]})
db.users.insert({_id: 102, likes: [
'pina coladas',
'long walks on the beach'
]})
db.users.insert({_id: 103, likes: [
'getting caught in the rain',
'bowling'
]})
db.users.insert({_id: 104, likes: [
'pina coladas',
'long walks on the beach',
'getting caught in the rain'
]})
您希望计算给定用户在本例中与其他用户有多少匹配的特性“喜欢”?以下聚合管道将实现这一点:
user = 100
user_likes = db.users.findOne({_id: user}).likes
return_only = 2 // number of matches to return
db.users.aggregate([
{$unwind: '$likes'},
{$match: {
$and: [
{_id: {$ne: user}},
{likes: {$in: user_likes}}
]
}},
{$group: {_id: '$_id', common: {$sum: 1}}},
{$sort: {common: -1}},
{$limit: return_only}
])
根据上面的示例输入数据,这将输出以下结果,显示前2个匹配项:
{
"result" : [
{
"_id" : 104,
"common" : 3
},
{
"_id" : 102,
"common" : 2
}
],
"ok" : 1
}
请注意,我假设您只需要最前面的匹配项,因为可能有大量用户。$sort步骤和$limit步骤将完成此操作。如果不是这样,那么您可以省略管道中的最后两个步骤
我希望这有帮助!如果您还有其他问题,请告诉我
布鲁斯如果我理解你的问题,你有如下数据:
db.users.insert({_id: 100, likes: [
'pina coladas',
'long walks on the beach',
'getting caught in the rain'
]})
db.users.insert({_id: 101, likes: [
'cheese',
'bowling',
'pina coladas'
]})
db.users.insert({_id: 102, likes: [
'pina coladas',
'long walks on the beach'
]})
db.users.insert({_id: 103, likes: [
'getting caught in the rain',
'bowling'
]})
db.users.insert({_id: 104, likes: [
'pina coladas',
'long walks on the beach',
'getting caught in the rain'
]})
您希望计算给定用户在本例中与其他用户有多少匹配的特性“喜欢”?以下聚合管道将实现这一点:
user = 100
user_likes = db.users.findOne({_id: user}).likes
return_only = 2 // number of matches to return
db.users.aggregate([
{$unwind: '$likes'},
{$match: {
$and: [
{_id: {$ne: user}},
{likes: {$in: user_likes}}
]
}},
{$group: {_id: '$_id', common: {$sum: 1}}},
{$sort: {common: -1}},
{$limit: return_only}
])
根据上面的示例输入数据,这将输出以下结果,显示前2个匹配项:
{
"result" : [
{
"_id" : 104,
"common" : 3
},
{
"_id" : 102,
"common" : 2
}
],
"ok" : 1
}
请注意,我假设您只需要最前面的匹配项,因为可能有大量用户。$sort步骤和$limit步骤将完成此操作。如果不是这样,那么您可以省略管道中的最后两个步骤
我希望这有帮助!如果您还有其他问题,请告诉我
Bruce从MongoDB 2.6+开始,您可以使用表达式 如果要求两个数组集的交集,首先需要使用操作符来查找两个数组集的交集。中给出了另一个示例
然后,您可以使用新操作符来获取管道的相交阶段的输出大小。提供了一个使用新$size表达式的示例。从MongoDB 2.6+开始,您可以使用该表达式 如果要求两个数组集的交集,首先需要使用操作符来查找两个数组集的交集。中给出了另一个示例
然后,您可以使用新操作符来获取管道的相交阶段的输出大小。提供了一个使用新的$size表达式的示例。您将对如何进行非零数组交集感兴趣。在聚合框架中?你是比较一对一的用户还是需要一对多的用户?你能提供一些示例文档以及你希望得到的结果吗?嘿,大家好,谢谢你们的快速反馈。这是一个一对多的比较,检查主用户的收藏夹数组与其他所有用户的收藏夹数组。对于非零交叉点,我只需$match匹配user.favorites$nin main.favorites的用户。当然还有德里克。我的输入文档是:{user:David,favorites:[1,2,3]}基本上,我希望我的输出是:{movie_id:2,score:12},其中分数由普通电影的数量加权,读取:主用户和其他用户之间的集合交集大小。只是为了澄清一下,收藏夹数组是movie_id。您将对如何进行非零数组交集感兴趣。在聚合框架中?你是比较一对一的用户还是需要一对多的用户?你能提供一些示例文档以及你希望得到的结果吗?嘿,大家好,谢谢你们的快速反馈。这是一个一对多的比较,检查主用户的收藏夹数组与其他所有用户的收藏夹数组。对于非零交叉点,我只需$match匹配user.favorites$nin main.favorites的用户。当然还有德里克。我的输入文档是:{user:David,favorites:[1,2,3]}基本上,我希望我的输出是:{movie_id:2,score:12},其中分数由普通电影的数量加权,读取:主用户和其他用户之间的集合交集大小。为了澄清,favorites数组是movie_id。