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

142
Visualizações
Firestore load document if user is a member

I have two collections: groups and users

I would like to perform a query that will return all groups where a user has a particular role assigned.

A group looks like this

{
  "name": "myGroupName",
  "members" : {
    "firstUsersID" :
      {
        "roles" : ["user", "admin"]
      },
      "secondUsersID" :
      {
        "roles" : ["user" ]
      },
  }
}

Is there a way to perform queries in react native/web SDK which will return

  1. All groups where a given user e.g firstUserID is listed in the members map/object
  2. All groups where a given user e.g firstUserId has a particular role e.g "admin"

Alternatively would I have to create an array/map on the user object listing their roles? Are there advantages/disadvantages to each route?

Currently I have the following which returns all groups

const colRef = collection(db, "groups");
const q = query(colRef)
const colSnap = await getDocs(q)
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I figured it out I think

The following works

const colRef = collection(db, "groups");
const q = query(colRef, where("users." + user.uid + ".roles", "array-contains", "admin"))

In this case I am dynamically getting the uid of the current authenticated user but you could also hardcode it using

const q = query(colRef, where("users.firstUserID.roles", "array-contains", "admin"))
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