I'd like to query some posts in firestore based on tags user entered. Whenever a user submits a post, the user also submits tags along with it. My current data structure looks like this:
postId: {
tags: {
"firebase": true,
"angular": true,
"cats": false
}
}
However, the above data structure makes it hard to query. I cannot use the arrayContainsAny function and would have to download all of the tags and check if each tag/key is true then return the posts. However, if I store the tags as an array:
postId: {
tags: {
0: "firebase",
1: "angular",
2: "cats"
}
}
I could use the firestore func like postsCollection.whereField("tags", arrayContainsAny: tagsArray) for fast querying.
Is it more efficient or scalable to store tags as an array?
Thanks in advance!
There is no right or wrong way to model data in NoSQL type databases. The best practice for modeling always depends on the queries you want to perform against it.
It sounds like using a map is not effective for your use case. You've already identified that it makes your query difficult, and using an array would be more convenient. In that case, go with the array, since you know that it satisfies your query.
Bear in mind that not every data model satisfies every kind of query. For this particular case, an array seems better, but you could easily run into a situation for another query where it doesn't work well at all.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With