문제

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top