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

    • Type: Icon: New Feature New Feature
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 2.4.3
    • Component/s: Querying

      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}})

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

              Created:
              Updated:
              Resolved: