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

304
Views
How to create a custom id document in firestore(Firebase modular SDK (v9))

How can we create a document with a custom id in firestore with the new modular SDK V9?

const register = async function (adminEmail){
  try{
    const docSnap = await getDoc(availableIdRef);
    const collegeId = docSnap.data().id; //This should be the document id

    const adminRef = collection(firestore, 'admin');
    await addDoc(adminRef, {adminEmail, collegeId});

  }catch(e){
    alert("Error!");
  }
}

I want to create a new document with id as collegeId in admin collection.

I know how to do this in web version 8(namespaced) but have no idea about web version 9(modular).

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

0

You can use setDoc() method to specify an ID instead of addDoc which will generates a random ID as mentioned in the documentation:

const adminRef = doc(firestore, 'admin', collegeId);
//       doc  --->^
await setDoc(adminRef, {email: adminEmail, id: collegeId}) // overwrites the doc if it already exists
//    ^^^

Also note that to create a DocumentReference you need to use doc() and not collection() which is used to create a CollectionReference. I've explained difference in these 2 here: Firestore: What's the pattern for adding new data in Web v9?

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!