How to select an array into document firestore?

Viewed 200

This seems to be a very bad thing I am about to ask .... But is there any way to select based on an array of facts ?

Example: I have a collection "User" and that user's Information, What I want to say is that inside User Information there is a Collection of Post and also the information of that Post.

Collection('user')->UidUser->InfoUser( Collection('Post')->UidPost->InfoPost, Name, ArrayFllower )

How can I paginate with Post with an ArrayFllower array?

If it is difficult to understand, let me go to the example again:

I am User A I have Follow User B and User C, I want to see the Post of User B and User C, what should I do? And I want to paginate with each time UserA (me) gets 3 Posts, then I scroll down to automatically paginate

enter image description here enter image description here

Update for my Question....

I used 2 queries => select Array Follow eg: arrayFollow = [uidUserB, uidUserC]. Then I use for to loop the Uids to get their Posts, but why can I only get Array Follow and arrayPost can't?

getData = async () => {
        const user = [];
        const Post = [];
        await Promise.all(
            await firebase.firestore().collection('user').doc(Fire.shared.uid).onSnapshot(documentSnapshot => {
                user.push(...documentSnapshot.data().Followings);
            })
        )
       for (const Uid of user) {
            await firebase.firestore().collection('user').doc(Uid).collection('Post').onSnapshot(querySnapshot => {
                querySnapshot.forEach(element => {
                    Post.push({ data: element.data() });
                });
            })
        }
        this.setState({ dataPost: Post });
 }
1 Answers

I would have chosen SQL database for your data model.

If you are currently under development, I suggest you to update your model by creating 2 collections, Users and Posts as it's way easier to select & query Posts (use UidUser to link User to Post).

If you aren't under development, You can create server side function like cloud function that will loop through all Post of UserA/B/C and return the first n Posts, n being page number parameter passed to cloud function.

How to gather Post : Collection group queries

  1. gather your followers uid in tab : ['uid-A', 'uid-B', 'uid-C']

  2. gather all posts from those followed users :

const querySnapshot = await db.collectionGroup('Post').where('uid', 'in', ['uid-A', 'uid-B', 'uid-C']).get();
querySnapshot.forEach((post) => {
  // get post data in post.data() :
  console.log(post.id, ' => ', post.data());
});
Related