[SERVER-9712] Is it possible to get array size returned as part of result or used for sorting Created: 16/May/13  Updated: 10/Dec/14  Resolved: 24/May/13

Status: Closed
Project: Core Server
Component/s: Querying
Affects Version/s: 2.4.3
Fix Version/s: None

Type: New Feature Priority: Major - P3
Reporter: Zhang Xiao Assignee: Unassigned
Resolution: Duplicate Votes: 0
Labels: array, size
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Duplicate
duplicates SERVER-478 Advanced $size quering Closed
Participants:

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



 Comments   
Comment by Ian Whalen (Inactive) [ 20/May/13 ]

x1a0 it looks like you want to extend the scope of SERVER-478 a bit? you should probably add your use case there as a comment.

Generated at Thu Feb 08 03:21:15 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.