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

249
Visualizações
How to conditionally retrieve current user's doc fields in Firestore

I need to conditionally retrieve fields of current user's doc. Here is my Firestore db structure.

I need all map type objects (setup1, setup2, setup3...) and their fields, nothing else.

It is possible to conditionally get the needed fields of current user's doc as follows:

const userRef = collection(db, 'users');
const q = query(userRef, where('uid', '==', userID), where('config1', '!=', false));
const userDocs = await getDocs(q);

But I cannot do that as users have only read permission for their own doc:

match /databases/{database}/documents {
  match /users/{uid} {
    allow read, write: if request.auth.uid == uid;
  }
}

In addition, it is not possible to use "where" without "query":

const userRef = doc(db, 'users', userID); // can't use "where" here
// const userRef = doc(collection(db, 'users'), userID) // same here
const userDoc = await getDoc(userRef);
setUserData(userDoc.data());

Is it possible to somehow use "where" above or to filter fields from userDoc.data()?

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

0

I need all map type objects (setup1, setup2, setup3...) and their fields, nothing else.

That's not possible using the Firebase Client SDK. You can only fetch the whole document. Firestore Node.JS SDK does support selecting required fields only but that is meant to be used on server side so you'll have to use Cloud functions or something similar.

But I cannot do that as users have only read permission for their own doc:

query(userRef, where('uid', '==', userID), where('config1', '!=', false));

This query will not return any document because there's no config1 field in the root of document.

Is it possible to somehow use "where" above or to filter fields from userDoc.data()?

It might best to create a sub-collection setups. Then you should be able to run all your required queries.

users -> { userId } -> setups -> { setupId }

If each setup sub-document has the configN field the following query should return all users setups where the given config is true:

const userSetupRef = collection(db, `users/${userId}/setups`);

const q = query(userSetupRef, where('config1', '!=', false));
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