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

Is it possible to get array size returned as part of result or used for sorting

    XMLWordPrintable

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Major - P3
    • Resolution: Duplicate
    • Affects Version/s: 2.4.3
    • Fix Version/s: None
    • Component/s: Querying
    • Labels:

      Description

      For example I have user documents with `badges` field:

      User

      {
          _id: "someone@example.com",
          password: "..."
          badges: ["b1", "b2", "b3"]
      }

      The `badges` field is updated by $addToSet so there won't be duplicate.

      Then I want to list users and sort by the number of badges they have. Currently I am using aggregation which can give me the result. however the $unwind step seems to be overkilled... as I understand it creates N documents on the fly if the user has N badges.

      It would be easier to make the query like:
      db.users.find({}, {$sort: {badges.$size: -1}})

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              x1a0 Zhang Xiao
              Participants:
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: