1

I have the following structure in Firestore for my chats collection:

chats: [
    chat1: { 
        jobs: [
            jobs/job1,
            jobs/job2,
            jobs/job4
        ]
    },
    chat2: { 
        jobs: [
            jobs/job2,
            jobs/job3
        ]
    }
]

If I'm on chat1, and I have the data, then I have the array of references to jobs. I know I have jobs/job1, jobs/job2 and jobs/job4.

How can I query and get the data for all of these jobs?

I could do: firestore.doc("jobs/job1").get() and firestore.doc("jobs/job2").get() and firestore.doc("jobs/job3").get() but that seems very inefficient.

And I don't understand how I can filter firestore.collection("jobs") to give me what I want.

I've seen a few other questions here asking the same thing, but for Java, and I'm unable to translate it into JavaScript.

morgoe
  • 350
  • 4
  • 16

2 Answers2

2

I could do: firestore.doc("jobs/job1").get() and firestore.doc("jobs/job2").get() and firestore.doc("jobs/job3").get() but that seems very inefficient.

That's exactly what you want to do - you can iterate the list and get each document individually. Firestore doesn't offer some optimized way to get multiple documents in one query using the web and mobile SDKs - it's plenty efficient just to get each one using its own query. You won't have any problems with this approach.

See also: Google Firestore - how to get document by multiple ids in one round trip?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
0
 import { deleteDoc, collection, doc, query, getDocs, setDoc } from "firebase/firestore"

const q = query(collection(db, 'collection_name'))
const querySnap = await getDocs(q)
querySnap.forEach((doc) => {

        console.log("querySnap data", doc.data())
        // reference is accessed as doc.ref
        // if u wish u can push them to an array like myDocRefs.push(doc.ref)


        console.log("querySnap data", doc.ref)

    })
Baraja Swargiary
  • 381
  • 2
  • 10