Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

285
Views
Firestore how to extract sub collection from collections

I have a data structure of like this.

here is image

Now i want to extract from product collection. i am already able to get its fields but don't known how to extract sub collections. Here is my code to extract product collection's field

useEffect(() => {
    async function fetchData() {
      const conditional_fetch = query(
        collection(db, "products"),
        where("active", "==", true)
      );
      const querySnapshot = await getDocs(conditional_fetch);
      const products = [];
      querySnapshot.forEach((doc) => {
        products[doc.id] = doc.data();
      });
      setProducts(products);
    }
    fetchData();

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You would have to make separate requests for each products prices sub-collection. So you could try something like this:

async function fetchData() {
  // 1. Fetching products
  const conditional_fetch = query(
    collection(db, "products"),
    where("active", "==", true)
  );

  const querySnapshot = await getDocs(conditional_fetch);
  const products = [];

  // 2. For each product, fetching documents from prices sub-collection
  for (const doc of querySnapshot.docs) {
    const prices = await getDocs(
      collection(doc.ref, "prices", "timestamp", "desc")
    );
    products.push({
      id: doc.id,
      ...doc.data(),
      prices: prices.docs.map(p => p.data())
    });
  }
  setProducts(products);
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!