简体   繁体   中英

Firebase query help for react-native

I'm kinda new to firebase & react-native but I need a little help with a query I have to do for my project.

So let's say I have 2 tables

Posts:{postID(key), posterID, description}
Likes:{likeID(key), postID, likerID}

so the relationship between these two tables is on postID

Is there a way to count all likes for each Post and return top 10 with most likes? I know that this process can't be all on server side so I'm fine with any solutions really.

I think this is the query to count likes for one post:

firebase.database().ref(`/likes`)
        .orderByChild('postId').equalTo(postID)
        .once('value', snapshot => {
            const likesCount = snapshot.count;

        })

Is there a way to count all likes for each Post...?

Firebase has no built-in aggregation queries. You'll typically either do this client-side, or through Cloud Functions, and write the count into each post. You'd then order on that "count property" to return the posts with the most likes.

Also see:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM