This is a migrated thread and some comments may be shown as answers.

Sort by array size

1 Answer 40 Views
JavaScript SDK
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Viktor
Top achievements
Rank 1
Viktor asked on 13 Jul 2017, 08:07 AM

Hi,

Is there a way to sort items by the size of an array field?

For example, if I have a content type Posts with field Likes which is an array and I want to get the top 20 most liked posts.

 

Thanks,

1 Answer, 1 is accepted

Sort by
0
Martin
Telerik team
answered on 17 Jul 2017, 10:25 AM

Hi Viktor,

I see your point. Currently this is not possible as the aggregate functions supported may count only items in the content type but not array in an item itself.

I would suggest to consider using one of the following:

  • Cloud function that gets all data from the content type (you may limit the fetched data only to the id and array field) and returns the Ids of the top 20 items with largest likes arrays. With this approach you would have to take into account the restrictions of cloud functions - if your content type and arrays become too big the max execution time might become an issue.

  • Introduce a separate field in your content type that holds just the number of items in the liked posts array. I believe this is the better approach since the data would be fetched much faster (no calculations would be needed), you would not have to be limited by the cloud functions restrictions and you may easily filter based on that field. The filed would hold just a number and you would increment that number each time a new items is added to the likes array:
    {
        LikesArray: [user_id_1, user_id_2, user_id_3, .... , user_id_99],
        LikesCount: 99
    }

Let me know if this has helped.

Regards,
Martin
Progress Telerik
 
Everlive is now Telerik Backend Services, and is part of the Telerik Platform.
 
Tags
JavaScript SDK
Asked by
Viktor
Top achievements
Rank 1
Answers by
Martin
Telerik team
Share this question
or