Reputation: 1
I am new to Firestore and NoSQL. I want to create book items where each book item belongs to only one category of many categories. While retrieving book items, I want books Items of each category separately. How can I model the database?
Upvotes: 0
Views: 567
Reputation: 3361
You can have all book items in a single collection. Each item will have a 'category' field. When you want to retrieve books with one particular category, you can use Firestore's built-in-indexing to efficiently retrieve just the books of a particular category by adding the .where() method on to the end of the query. If you only want to retrieve a certain number of books of that category you can use the .limit() method on your query.
If you are chaining conditions onto your query, like multiple .where()'s or .orderBy()'s, you will need to add a custom index to your database so it knows how to organize things and where to look for a particular query. You can manage these indices in the firestore console, it is next to the "database" and "rules" tabs.
IMO this indexing is the coolest thing about firestore. If you have a million documents in your collection, but you build a query for that collection that only returns three documents, it will be as fast and affordable as if those were the only three documents in the collection.
More info on indexing: https://firebase.google.com/docs/firestore/query-data/indexing
And their guide on building your queries: https://firebase.google.com/docs/firestore/query-data/queries
Upvotes: 1