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

124
Views
Listen to realtime updates of a document inside an array in an object with Firestore (react)

thanks in advance for your time.

Im trying to listen to realtime updates of a document which lives deep inside after some nested levels.

Is this possible or I should listen to the whole first-level document changes and do the filtering on the client?

This is how my database structure looks:

articles: { 
      'article-one': {
          author: "jane doe",
          status: "stock",
          similar: [
             {articleId: "article20", type: "sold"},
             {articleId: "article21", type: "stock"},
             ...
         ]
      }
   }

Im trying to listen to realtime updates on the article with 'articleId' of "article20" inside the SIMILAR array.

This is what im trying:

const myQuery = query(collection(db, "articles", article-one, "similar"), where("articleId", "==", "article20"));

const unsub = onSnapshot(myQuery, (querySnapshot) => {   
               const selectedArticles = []
                querySnapshot.forEach((doc) => {
                    selectedArticles.push(doc.data())
                })   
                console.log(selectedArticles);  //output: []  
            })
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If you are trying to query elements from an array then that's not possible at the moment. You would have to do either of the following:

  1. Fetch the article's documents and filter the array field using Javascript as shown below:
const myQuery = doc(db, "articles", article-one);

const unsub = onSnapshot(myQuery, (docSnapshot) => {  
  const similar = docSnapshot.data().similar 
  const selectedArticles = similar.filter(a => a.articleId === "article20")
  console.log(selectedArticles);  
})
  1. Create a sub-collection "similar" and then use existing query. That should then return documents of all similar articles with given articleId.
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!