Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

822
Visualizações
How to add Document with Custom ID to firestore

Is there any chance to add a document to firestore collection with custom generated id, not the id generated by firestore engine?

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

To use a custom ID you need to use .set, rather than .add

This creates a document with the ID "LA":

db.collection("cities").doc("LA").set({
    name: "Los Angeles",
    state: "CA",
    country: "USA"
})

This is taken from the official docs here

over 4 years ago · Santiago Trujillo Relatório

0

In case if you are using angularfire,

class Component{
  constructor(private afs: AngularFireStore) {} // imported from @angular/fire/firestore

  addItem() {
    this.afs.collection('[your collection]').doc('[your ID]').set({your: "document"});
  }
}
over 4 years ago · Santiago Trujillo Relatório

0

To expand on the accepted answer, if you ever wanted your client to generate a random ID for the doc before pushing to Firestore (assuming the same createId() function exists outside of AngularFire2)

const newId = db.createId();
db.collection("cities").doc(newId).set({
    name: "Los Angeles",
    state: "CA",
    country: "USA"
})

This is useful for setting the ID as a reference field in another document, even before Firestore saves anything. If you don't need to use the saved object right away, this speeds up the process by not making you wait for the ID. The set() call is now asynchronous from the pipe you might be using in Angular

Notice I didn't put id: newId in the set object, because Firestore by default doesn't save ID as a field in the doc

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda