Are we able to sort by a related document's field with the REST API?

default discord avatar
allannnc
7 months ago
3

Hey! I'm looking to do a REST query like


/api/ticket/${qs.stringify(
              {
                where: {
                  user: {
                    equals: currentUser.id,
                  },
                },
                sort: 'event.startsAt',
                depth: 1, // get event data too
              },
              { addQueryPrefix: true }
            )}

But it doesn't seem like sorting by a relationship's nested field is possible. So I've gone with a frontend .sort() after querying instead



Just want to confirm that my understanding is correct here (we can't sort by a related doc's field)

  • default discord avatar
    notchr
    7 months ago

    @allannnc I'm not sure about doing this with builtins, but it may exist. Alternatively, you could add an entry to the collections "endpoints" array so that you can manually sort the data and send it back

  • default discord avatar
    allannnc
    7 months ago

    Thanks for the reply! I think I'll stick with the frontend sort as it ends up being the same amount of work anyway

  • default discord avatar
    notchr
    7 months ago

    That's fair! If it were a large amount of items, I would say it's faster on the backend, otherwise a frontend sort works!

Open the post
Continue the discussion in Discord
Like what we're doing?
Star us on GitHub!

Star

Connect with the Payload Community on Discord

Discord

online

Can't find what you're looking for?

Get help straight from the Payload team with an Enterprise License.