How to get Count of Related Data

Hi there

I have a table of doctors, and second table is of prescriptions.
I want to show doctors screen with lets say 50 doctor/page, and i also want to show number of prescriptions prescribed by each doctor.

How can i achieve that 1 call?

If multiple, then how?

Regards

Hi ARafay,

Here’s an example that shows how to load objects (doctors in your case) with a collection of related entities (prescriptions):
https://backendless.com/feature-22-loading-related-data-objects-the-one-stepdynamic-approach/

To see the number of prescriptions for a doctor you can just check the size of the collection of the related entity.

Regards,
Mark

but that’d be overkill.

we usually have doctors with over 100 of prescriptions / month and loading all those prescriptions for just count is not needed.

Is there any other efficient way?

You can get a count with a separate query for a doctor using BackendlessDataQuery with PageSize = 1 in QueryOptions, but that would require a separate call for every doctor.

this method is still a big No, we will have to make 50 extra calls for showing 50 doctors.
Is there any planned feature to solve this?
There are other BAAS that solve this issue by providing Graph Queries/APIs.

There are other ways to “solve” this. You can modify the data model to include the information without calculating the totals every time. You can create a server-side service to optimize the retrieval.

Currently there are no plans to offer graph queries

Regards,
Mark

Serverside service = Serverside Script on Backendless Server?

What you can do is provide aggregateable properties, some clouds do that too.
So that property will be based on table’s relations with functions like count, average etc

so if i create a new aggregate property like total_prescriptions, then it will be automatically updated whenever a new prescription is added or deleted.

By “server-side services” I meant the functionality documented at:

https://backendless.com/documentation/api-engine/apieng_overview.htm

Regards,
Mark

Hi, I just came across this thread and I have this exact issue. Is there a new way to solve this problem? The discussion was from many years ago.

Hi @Barry

Here is a few links that describe how you can load objects count

https://backendless.com/docs/rest/data_get_object_count.html
https://backendless.com/docs/rest/data_count.html

Regards,
Vlad