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

121
Visualizações
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 Respostas
Responde à pergunta

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 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