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: []
})
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:
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);
})
articleId.