Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

256
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!