Supposedly I have a database structure like this
Person ('name', 'age')
Belonging ('person', 'type')
with its relationship like this
Person -> Belongings (one to many)
and I want to query all person with their number of belongings included like this
Array[0] -> name, age, numberOfBelongings,
Array[1] -> name, age, numberOfBelongings,
etc
How do I do this? I tried to use CloudCode but, it turned out the fetching method is asynchronous, so I can't fetch the number of belongings to each person.
You can't do that directly. If you need this count, you could create a field on the Person class (i.e. numberofbelongings) which you update in an afterSave cloud code hook on Belonging. Whenever a Belonging is saved, you also increment the numberofbelongings field on the Person object, and save that as well.
No extra queries, no extra network traffic from your device, and your always have the count available for any Person object.