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

Ability to use Limit() with Distinct()

    Details

      Description

      Ability to use Limit() with Distinct()... or any of the cursor methods for the matter (sort, count, skip, etc. although limit has the biggest impact).

      Apparently there is no simple way to currently perform the following SQL query:

      SELECT DISTINCT(myField) FROM myTable WHERE x = y LIMIT(20)

      The current Distinct() implementation only allows for bringing back ALL distinct values in the collection or matching a query, but there is no way to limit these results. This would be very convenient and there are many use cases.

        Activity

        Hide
        ivan.fioravanti@4ward.it Ivan Fioravanti added a comment -

        Any news on this feature request? It is quite useful in some scenario.

        Show
        ivan.fioravanti@4ward.it Ivan Fioravanti added a comment - Any news on this feature request? It is quite useful in some scenario.
        Hide
        mozillazg mozillazg added a comment -

        Is there any news about this feature request?

        Show
        mozillazg mozillazg added a comment - Is there any news about this feature request?
        Hide
        asya Asya Kamsky added a comment -

        This can be done using aggregation framework as

        db.collection.aggregate([{$match:{<your-filter>}}, {$group:{_id:null, myField:{$addToSet:"$myField"}}},{$limit:20}])

        Show
        asya Asya Kamsky added a comment - This can be done using aggregation framework as db.collection.aggregate([{$match:{<your-filter>}}, {$group:{_id:null, myField:{$addToSet:"$myField"}}},{$limit:20}])
        Hide
        axom Brian Riley added a comment -

        Asya's suggestion may have worked in a past iteration of mongo, but at least currently you will need to adjust the group stage to get the limit to do anything.

        {$group: {_id: "$myField"}}

        Show
        axom Brian Riley added a comment - Asya's suggestion may have worked in a past iteration of mongo, but at least currently you will need to adjust the group stage to get the limit to do anything. {$group: {_id: "$myField"}}
        Hide
        asya Asya Kamsky added a comment -

        Brian Riley you absolutely correct, I will fix or remove mine.

        Show
        asya Asya Kamsky added a comment - Brian Riley you absolutely correct, I will fix or remove mine.

          People

          • Votes:
            47 Vote for this issue
            Watchers:
            36 Start watching this issue

            Dates

            • Created:
              Updated:
              Days since reply:
              27 weeks ago
              Date of 1st Reply: