Best approach for sorting by relationship count

Hi, first time here.

Context: I’m using the latest strapi version with mongodb and graphql plugin.

I think my scenario can be a common thing, so maybe I’m missing something.
I have a blog-article type and a blog-comment type, so my blog-article has a blog_comments field for the relationship.
I want to be able to sort the articles by the number of published (comments with published_at not being null) comments. Is that possible? If so, how?

So, I can only think of two options:

  1. Create a custom mongoose query in the blog_comments service.
  2. Create a comments_count field and update it at some point (services or model, don’t now) on every article create/update and comment create/update/delete.

Which is the way to go?

Thanks.

1 Like

Hello !

Did you find a solution ?

I also am curious to know what solution you came up with, @miquelromero.

It’s very annoying that people raise a question/issue but then never respond to anyone else posting comments on it; my guess is that they did not find a solution.

I have trying to figure out how I can order by relationship count via the REST API, seems there is no way to do this. I am now looking at customising the controllers/services to achieve this, once again though, seems like it’s impossible to do this.

It seems very odd that something as simple as ordering by relationship counts or selecting relationship counts as a field is not possible with Strapi, it’s one of the most basic and very commonly used things.

Has anyone found a solution to this yet, or is it currently impossible with Strapi?

For me I am trying to select blog post categories/tags with a limit of 5 and ordered by how many blog posts are assigned to them. I am doing this to show a list of categories that are most commonly used rather than showing a massive list.

To achieve this, since there isn’t a built in way yet, I query the categories and populate it’s blogPosts relation field but only select the id property in it’s fields (I don’t need any actual data from the blog posts in this instance). Then I sort the results in code by length of blogPosts reversed (desc order) and then select only 5 of them for return.

It’s not idea as the query is returning back an array of blog posts for each category (albeit only with an id field) and I’ve got to do some mapping code.

Really Strapi should allow for a special count property when filtering and sorting by relationship fields and then it’s a simple API request only.

It’s very annoying that people raise a question/issue but then never respond to anyone else posting comments on it; my guess is that they did not find a solution.

I’m sorry but:

  • I didn’t get an answer until long time later.
  • I’m barely using Strapi nowadays.
  • Strapi forum emails were going to spam. :sweat_smile:

Regarding the solution, AFAIR I ended up doing the second option. One of the interesting things of Strapi is not needing to build queries manually, so I preferred to have redundant data while being able to just rely on graphql queries (with lower cost).

I posted a workaround solution here: Relation field count & ordering by relation count - #9 by szilagyi-sandor