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

175
Visualizações
Firestore: What's the pattern for adding new data in Web v9?

I have seen in many places that to access nested documents and collections the pattern is something like db.collection("users").doc("frank").collection("pets") etc.

This makes a lot of sense to me and is easy to understand. The trouble is, my (React) project is set up in the Web version 9 way. I've combed the docs over and over and can't see anything that goes beyond referencing X doc in Y collection.

I need to reference Users > uid > someCollection

But in the Web Version 9 way I can only do: doc(db, "users", uid)

How do I go deeper?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you are trying to get:

  • a CollectionReference, then use collection():
const myCol = collection(db, "collection", "doc1", "sub-col1")
  • a DocumentReference, then use doc():
const myDoc = doc(db, "collection", "doc1", "sub-col1", "sub-doc1")

The concept remains same. The path to a document has even number of segments e.g. col/doc/sub-col/sub-doc while path to a collection has odd e.g. col/doc/sub-col.

Both the methods will throw an error if invalid number of parameters are passed.


In the name-spaced version (v8), it used to look like:

// DocumentReference
firebase.firestore().doc("col/doc/sub-col/sub-doc")

// CollectionReference
firebase.firestore().collection("col/doc/sub-col")

In essence, you keep adding path segments to the same doc() or collection() methods.

doc(firestore: Firestore, path: string, ...pathSegments: string[]):
// You can also use spread operator with an array

An example with spread operator:

const myDocPath = "users/user1/posts/post1/comments/comment1"
const docRef = doc(db, ...myDocPath.split("/"))

Just make sure you don't have any leading or trailing slash if using spread operator with split().

about 4 years ago · Juan Pablo Isaza 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