Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

263
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda