Uploaded image for project: 'Core Server'
  1. Core Server
  2. SERVER-72121

Add $top/$bottom/$topN/$bottomN as expressions

    • Query Execution

      It is rather awkward that to get topN array elements, you would do $sortArray+$slice while there's topN accumulator available.

      Proposing: 

      $topN: {input: <expression resolving to array>, n: <n expression>, sortBy: <spec>}

      e.g.:

      [
        {
          $set: {
            topThreeOptions: {
              $topN: {
                input: "$options",
                n: 3,
                sortBy: { votes: -1 },
              },
            },
            fiveWatchersAZ: {
              $topN: {
                input: "$watchers",
                n: 5,
                sortBy: 1
              },
            },
          },
        },
      ]

            Assignee:
            backlog-query-execution [DO NOT USE] Backlog - Query Execution
            Reporter:
            kateryna.kamenieva@mongodb.com Katya Kamenieva
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: