从数组中查找前N个条目 [英] Finding top N entries from the Array
问题描述
我的收藏集的结构如下:
My collection is structured like this:
{
"_id": 1,
"Trips": [
{
"EndID": 5,
"Tripcount": 12
},
{
"EndID": 6,
"Tripcount": 19
}
],
"_id": 2,
"Trips": [
{
"EndID": 4,
"Tripcount": 12
},
{
"EndID": 5,
"Tripcount": 19
}
], ...
}
可以看出,每个文档都有一个Trips
数组.现在,我要查找的是集合中所有文档中所有Trips
数组的 N个Tripcounts .有可能吗?
As it can be seen, every document has a Trips
array. Now what I want to find, is the top N Tripcounts of all the Trips
arrays combined across the documents in the collection. Is that possible?
我已经有了以下内容,但是这仅从每个Trips
数组中获取单个最大Tripcount
,然后输出其中的50个.因此,实际上在一个Trips
数组中具有前2个行程会导致该查询删除第二个旅程:
I already have the following, however this only takes the single greatest Tripcount
from each Trips
array and then outputs 50 of them. So actually having the top 2 trips in one Trips
array results in this query dropping the second one:
var group = db.eplat1.aggregate([
{ "$unwind": "$Trips"},
{ "$sort": {
"Trips.Tripcount": -1
}
},
{ "$limit": 50 },
{ "$group": {
"_id": 1,
"Trips": {
"$push": {
"Start": "$_id",
"Trips": "$Trips"
}
}
}}
], {allowDiskUse: true})
请注意,我认为此问题不同于该问题,只有一个文件.
Note that I believe this problem is different to this one, as there only one document is given.
推荐答案
基本上,您需要对数组元素进行排序( $sort
/ $group
)和那么您可以对顶部的值进行 $sort
并 $limit
结果.
Basically you need to sort the array elements ($unwind
/$sort
/$group
) and then you can do your $sort
for the top values and $limit
the results.
最后,您 $slice
"在数组中的文档中.
Finally you $slice
for the "top N" in the documents in the array.
db.eplat1.aggregate([
{ "$unwind": "$Trips" },
{ "$sort": { "_id": 1, "Tips.TripCount": -1 } },
{ "$group": {
"_id": "$_id",
"Trips": { "$push": "$Trips" },
"maxTrip": { "$max": "$Trips.TripCount" }
}},
{ "$sort": { "maxTrip": -1 } },
{ "$limit": 50 },
{ "$addFields": { "Trips": { "$slice": [ "$Trips", 0 , 2 ] } } }
])
这篇关于从数组中查找前N个条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!