I am trying to query firebase data that is in the third level.
I am using the following code.
from(this.firestoreDb
.collection('userPosts')
.doc('123')
.collection('posts', ref => ref.where('id', '==', 999))
.snapshotChanges())
.subscribe(res => {
console.log(res);
});
I am trying to query for the content but the above code returns empty array. I am able to fetch all the posts just using the .doc(123) but not a specific post. I also need help to update each post with comments. Kindly help.
The .where("id", "==", 999)
will try to find a document in userPosts
collection where the field id
is equal to 999
and not search through arrays. There is no way you can query using any field in an array (unless you know the whole object as is). You would have to convert it to a map or use sub-collections if you need such queries.
You can just store the posts directly in your userPosts
collection and each document in it would be a post. Just make sure you have the userID and postID in that document. The document may look something like:
{
userID: "user_id",
postID: "post_id",
...otherDocFields
}
Now you can easily query posts made by a user or a post with given ID:
from(this.firestoreDb
.collection('userPosts', ref => ref.where('postID', '==', 999))
.snapshotChanges())
.subscribe(res => {
console.log(res);
});