I think that I know answer to my question but still need confirmation :)
I have collection 'cars' and 'feed'.
Cars are all data about car and feed is collection of short info about car: model, manufacturer, photo, year etc.
Cars documents has also category
and subCategories
which I don't need in feed documents at least not to show them.
But I also can filter feed documents by categories and subCategories.
Now I understand that if I need to show just photo/model/manufacturer to the user (based on how I understand nosql) there is no need to download that additional data which not even be visible but I need them to make filtering possible.
My question is am I overthinking about this or if category and subcategories should not be part of feed document how to make filtering possible in this case?
Asked
Active
Viewed 88 times
0

1110
- 7,829
- 55
- 176
- 334
-
We are usually structuring a Firestore database according to the queries that we want to perform. What are those queries? – Alex Mamo Jul 18 '22 at 06:41
-
Well In app I have list that can load infinite paged number of cars and clicking on it user can see details. But user can also filter that list without entering details. So my first thought is to use only one collection cars and use it for list and details. But I am not sure should I optimize it and when make car add less details in separate collection for list by duplicate data. Which then can lead me to later update in teo places if want to add for example new subcategory or tag for car. – 1110 Jul 18 '22 at 07:52
-
I think that you should take into consideration this [answer](https://stackoverflow.com/a/54258505/5246885). – Alex Mamo Jul 18 '22 at 07:55
1 Answers
0
Based on your use-case, I would suggest maximizing the Reference Data Type, references are very much like foreign keys. You can use references in queries like any other value: for filtering, ordering, and pagination.
Lets Assume that this is your data structure:
feed
|- document (documentID = "a")
|- model (string)
|- manufacturer (string)
|- photo (string)
|- year (number)
|- reference (reference): "cars/b"
cars
|- document (documentID = "b")
|- data (string)
|- other data (string)
|- category
|- subcategories
Here, cars
document has a reference to a document in the feed
collection. We can now use the following sample code segment to get the feed and then retrieve the other details from the cars` collection by using the reference.
import { doc, query, collection, where, getDocs } from "firebase/firestore";
const carsDocRef = doc(db, "b");
const q = query(
collection(db, "feed"),
where("reference", "==", carsDocRef)
);
const data = await getDocs(q);
I've used Firebase Web Version 9 in my example but the general idea is you can use a reference data type to remove redundant data/information from the documents which should be not a part of it.
These documentations might help you:

Marc Anthony B
- 3,635
- 2
- 4
- 19
-
Thanks, but in this query how I can add category or subcategories filter? – 1110 Jul 18 '22 at 06:47
-
You then just have to add a `where()` query on `carsDocRef` so that you can filter categories or subcategories based on the query that you will create. – Marc Anthony B Jul 18 '22 at 22:29