Search code examples
parse-platform

Parse.com how to do sorting and query on included ParseObject?


I'm trying to do sorting and querying on a list from Parse:

new Parse.Query('StoreCard')
  .include('user') // user is a Pointer to the User class
  .matches('user.name', new RegExp('abc', 'i')
  .ascending('user.name');

However I've been trying for so long to make it work when having to query on included ParseObjects such as user in this case, but without success.

Getting it to work on non-included properties is no problem such as this example:

new Parse.Query('StoreCard')
  .include('user') // user is a Pointer to the User class
  .matches('storecardName', new RegExp('abc', 'i')
  .ascending('storecardName');

I can't find any examples nor documentation on how to achieve what I need..

Does anyone know what I should do?


Solution

  • It can't be done without adjustment to the model. If the data is small-ish, you could do the sort on the client or in a cloud function. For something time-critical, bigger, that might be paginated, I suggest adding a userName property to the StoreCard, and setting it in a beforeSave trigger on StoreCard.

    You may also discover that you need only the userName and a small set of other user fields on that StoreCard, in which case you can denormalize all of them and skip the .include query, too.