'How to get data from multiple Id's in firestore in react native

I will get the data from multiple id's in firestore.

here is code:

const array = ['0phkMIPUevw9Ou7MXwIK', '0vWgD1ZdJmdR8zbQ2vba'];
    const rs1 = await firestore().collection('feed').doc(array).get()
    console.log("rs1 ---> ", rs1.data()) 


Solution 1:[1]

Firestore doesn't expose API to fetch multiple documents in a single batch.

You can achieve that by fetching each document individually and merging results together into a single array.

async function fetchFeedPosts(postIds = []) {
  const promises = postIds.map(async (postId) => {
    const docSnapshot = await firestore().collection("feed").doc(postId).get();
    const docData = docSnapshot.data();

    return docData;
  });

  // Resolve all posts promise result into single array
  const feedPosts = await Promise.all(promises);

  return feedPosts;
}

// Call function later in your code

const postIds = ["0phkMIPUevw9Ou7MXwIK", "0vWgD1ZdJmdR8zbQ2vba"];

const feedPosts = await fetchFeedPosts(postIds);



Solution 2:[2]

Firestore does allow for some queries, I believe this could serve as an alternative if the postIds array isn't too large

const postIds = ["0phkMIPUevw9Ou7MXwIK", "0vWgD1ZdJmdR8zbQ2vba"];

const querySnapshot = await firestore().collection("feed").where('__name__', 'in' ,postIds).get();

const postsArray = querySnapshot.map((doc)=>doc.data())

https://firebase.google.com/docs/firestore/query-data/queries#web-version-9_6 https://firebase.google.com/docs/reference/rules/rules.firestore.Resource

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shy Penguin
Solution 2 RodSar