How to retrieve all product category from database?

I have, for example, 10 000 different products, and they belong to the 300 categories (electronics, food, toys...).
The question, as in this case the right to select the list of categories in the database mongodb? I have the option to create a separate model for categories, and each adding new product to check whether such a category, and if not, append to the array of categories. Here the load on the database, I think would be minimal. There is another option, whenever it is necessary to obtain a list of products to sample based on all products (cecati categories in them). But I don't really understand how to describe in code. I will not take all items, then filter on the server and give to the client.
How better to do such things?
April 19th 20 at 12:36
1 answer
April 19th 20 at 12:38
so you can get all the unique values
https://docs.mongodb.com/manual/reference/method/d...

create a separate model for categories, and each adding new product to check whether such a category, and if not, append to the array of categories

Yes, and in the paper product to store the _id from the collection of categories, and then it will be possible in a single query with $lookup to pull and the product name and its category, for example, but in General it may be better to think about sql

Find more questions by tags MongoDB