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

257
Visualizações
Query Nested Object in Firebase Firestore

I have in my Firestore database a list of documents that include this field 'Participants', as a nested object.

database screen shot

I want to make a query that gets only one document from the database (to see if it exists or not) that has (for example) user id 5 and 6.

This is what my code looks like

const chatsCollection =  db.collection('chats');

async function createChat(myId, otherUserId){

  chat = await chatsCollection
  .where(`participants.${myId}`, "==", true)
  .where(`participants.${otherUserId}`, "==", true)
  .limit(1).get();

  if(!chat.exists){
     alert('chat doesnt exist')
     //create chat
  } else {
     alert('chat exists')
     //do something else
  }

}

However, even if the chat with the participants object does indeed exist in the database, the result of the code indicates that it doesn't.

Here is the structure of the data when it is added to the database

    var chat_key = (Math.random() + 1).toString(36).substring(2);
    
    chatData = {
        key: chat_key,
        created_at: new Date(),
        participants: {
            myId: true,
            otherUserId: true,
        }
    }

    chatsCollection.doc(chat_key).set(chatData);

I appreciate any help on how to solve this problem. Thanks :)

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

0

on method collection, you can use empty property to see if the query getting data or not

if(chat.empty){
 alert('chat doesnt exist')
 //create chat
} else {
 alert('chat exists')
 //do something else
}

to get only one document from your query, you can use

chat.docs[0].data();
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