查询MongoDB数组并使用最匹配的元素进行排序 [英] Query MongoDB array and sort with the most matched elements
问题描述
在以下情况下,我需要您的专业知识.
I need your expertise on the following situation.
我有这样的收藏夹:
"array" : {
"item" : 1,
"1" : [100, 130, 255],
}
"array" : {
"item" : 2,
"1" " [0, 70, 120],
}
"array" : {
"item" : 3,
"1" : [100, 90, 140],
}
我正在这样查询这个集合:
I am querying this collection as such:
db.test.find(array.1 : {$in : [100, 80, 140]});
这将返回项目编号1和3,因为它将提供的数组中的任何值与集合中的值进行匹配. 但是,我想对该数组进行排序,以便为我提供更多相似数字的结果. 结果应分别为项目3和项目1.
This returns me item number 1 and 3 since it matches any values in the provided array with the ones in the collection. However I would like to sort this array to give me the results with more similar numbers. The result should be items 3 and 1 respectively.
但是,我可以获取结果并使用k最近邻算法对数组进行排序.但是,处理庞大的数据集使此操作非常不可取(或者是?) MongoDB中是否有提供此功能的功能? 我正在使用Java,是否有足够的算法来实现这一目标? 感谢您的帮助.
I can however grab the results and use a k-nearest neighbor algorithm to sort the array. However dealing with huge datasets makes this very undesirable (or is it?) Are there any functions within MongoDB to provide this? I am using Java, any algorithms to achieve this fast enough? Any help is appreciated.
谢谢.
推荐答案
您可以使用聚合框架执行此操作,尽管这并不容易.问题在于没有$in
运算符作为聚合框架的一部分.因此,您必须以编程方式匹配数组中的每个项目,这变得非常混乱. edit :如果$in
帮助您过滤掉大部分内容,则重新排列顺序,以使匹配项排在第一位.
You can do this with the aggregation framework, although it's not easy. The trouble lies with there not being an $in
operator as part of the aggregation framework. So you have to programatically match each of the items in the array, which gets very messy. edit: reordered so that the match is first, in case that $in
helps you filter a good portion out.
db.test.aggregate(
{$match:{"array.1":{$in:[100, 140,80]}}}, // filter to the ones that match
{$unwind:"$array.1"}, // unwinds the array so we can match the items individually
{$group: { // groups the array back, but adds a count for the number of matches
_id:"$_id",
matches:{
$sum:{
$cond:[
{$eq:["$array.1", 100]},
1,
{$cond:[
{$eq:["$array.1", 140]},
1,
{$cond:[
{$eq:["$array.1", 80]},
1,
0
]
}
]
}
]
}
},
item:{$first:"$array.item"},
"1":{$push:"$array.1"}
}
},
{$sort:{matches:-1}}, // sorts by the number of matches descending
{$project:{matches:1, array:{item:"$item", 1:"$1"}}} // rebuilds the original structure
);
输出:
{
"result" : [
{
"_id" : ObjectId("50614c02162d92b4fbfa4448"),
"matches" : 2,
"array" : {
"item" : 3,
"1" : [
100,
90,
140
]
}
},
{
"_id" : ObjectId("50614bb2162d92b4fbfa4446"),
"matches" : 1,
"array" : {
"item" : 1,
"1" : [
100,
130,
255
]
}
}
],
"ok" : 1
}
如果最后将matches
字段排除在$project
之外,则可以将其保留在结果之外.
You can leave the matches
field out of the result if you leave it out of the $project
at the end.
这篇关于查询MongoDB数组并使用最匹配的元素进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!