#Getting Related Data with `groupBy` query

5 messages · Page 1 of 1 (latest)

echo roost
#

I'm using groupBy to retrieve training logs, grouped by user -- using the REST API. The user is a record in the directus_users collection. The query looks like this:

https://myproject.directus.app/items/training_logs?groupBy[]=user&aggregate[count]=date

It returns something like this:

{
  "data": [
      {
          "user": "1fe56e5c-3ee7-4a11-9105-8eeaabc76f99",
          "count": {
              "date": "1"
          }
      },
      {
          "user": "45543bd7-f435-459d-923b-003b64331fd6",
          "count": {
              "date": "9"
          }
      }
  ]
}

However, I'm trying to return the user's first_name and last_name. The user making the call has full READ access to the directus_users collection. How can I modify the API call to retrieve related items for display purposes?

unreal orchidBOT
#

Thanks for posting! This is a community powered server, so you may or may not get an answer based on available help and expertise. To increase your chances of somebody being able to help you, please help us help you making sure you:

  • Adding an explanation of exactly what you're trying to achieve.
  • Adding any and all related code or previous attempts.
  • Describing the exact issue or error you are facing.
  • Posting any screenshots if applicable.
  • Reading through https://stackoverflow.com/help/how-to-ask.

When you're done with this thread, please close it. Thanks! ✨

(If you have a support agreement and need help, please contact the core team via email.)

hallow swan
echo roost
#

Thanks Jonathan. I have tried the fields parameter which works great for normal queries. However, it has no effect when doing a groupBy -- at least in my testing.

I found this post by rijkvanzanten on a v.9 release candidate post, stating you can't get relational data: https://github.com/directus/directus/discussions/8255

Assuming that's still the case, am I going about this incorrectly? A UUID is of little value to an end user. Is there an accepted approach to this problem? Do I have to separately query each User ID after my groupBy call to get the User's name?

GitHub

I am on directus v9.0.0-rc.93. I want to use aggregation, grouping and alias in API request. The thing is I am doing as per the doc and none of them are working individually or all together. Here i...

hallow swan
#

Sorry, you are correct the fields don't work with Aggregates -- assuming you are using a script for this you could get the user ids from the aggregate as an array and do a single query for users "_in" the array and get the names and any other data from the user OR iterate and get each one as you suggested