从数组中查找前 N 个条目 [英] Finding top N entries from the Array

查看:25
本文介绍了从数组中查找前 N 个条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的收藏结构如下:

{
    "_id": 1,
    "Trips": [
        {
            "EndID": 5,
            "Tripcount": 12
        },
        {
            "EndID": 6,
            "Tripcount": 19
        }
     ],
     "_id": 2,
     "Trips": [
        {
            "EndID": 4,
            "Tripcount": 12
        },
        {
            "EndID": 5,
            "Tripcount": 19
        }
     ], ...
}

可以看出,每个文档都有一个 Trips 数组.现在我要查找的是集合中文档中所有 Trips 数组组合 的 top 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 数组中获取 single 最大的 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.

推荐答案

基本上你需要对数组元素进行排序($unwind/$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 用于数组中文档中的前 N 个".

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆