I am creating a Firebase cloud function and would like to get the ID of a document from a collection by checking it's key values.
I have a collection with several documents. Each document has a unique company_number which is different from the document ID. I would like to get the document ID and store it in a constant.
const company_id = admin.firestore()
.collection('company_collection')
.where('company_number', '==', 'company_number')
I know my code is wrong but dont know how to correct it. I am new to Firebase and any help will be appreciated.
Thanks,
It seems the company_number is unique to each document so you can read ID of first document in the returned QuerySnapshot as shown below:
const companyDoc = await admin.firestore()
.collection('company_collection')
.where('company_number', '==', 'company_number')
.get()
// Check if that 1 doc is returned
if (companyDoc.empty) return "Document Not Found"
const company_id = companyDoc.docs[0].id