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

161
Visualizações
Get Firestore Object Attributes From Array

I used to have a document with the following structure:

OldDoc:

members: ['userId1', 'userId2', 'userId3']

I then wrote a query to retrieve all documents relevant to a user:

const docsSnapshot = await getDocs(query(
    collection(db, 'organisations'),
    where('members', 'array-contains', user.uid)
))

This worked well, but now I need to store the user's names in as well as:

members: [{ id: 'userId1', name: 'John Smith' }, { id: 'userId2', name: 'Jane Smith' }]

The challenge I'm facing is that I'm not sure how to construct a query that would yield the same results as the first one - i.e., how do I retrieve all docs where any of the objects have an id attribute corresponding to user.uid.

I tried using: where('members.id', '==', user.uid) but that didn't work.

Can a query be constructed to do this?

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

0

Firestore isn't great at these kinds of queries... I'd consider restructuring your member array to be an object with user.id as keys.

members : {
   userId1: { name: "fred" },
   userId2: { name: "martha" }
}

Another way, and perhaps more closely aligned to how Firestore 'wants' you to do it, would be to create another subcollection members with a new document for each member with the doc.id set to the user.id.

This approach also allows you to easily add more data to each member, with the added benefit of making queries on members that don't just rely on the user.id.

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