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

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

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