How to get all categories for a product in MongoDB

I want to have an efficient query witch will give me all products and it’s categories from 500k docs collection.

It should be a search by product title query which return something like this

 products: [{},{}],
 availableCategories: ["Phones", "Tools"], 
 total_records_found: 1000

For now I have this query and thinking how to extend it

db.products.aggregate([
{$match: { $text: { $search: "Iphone 11 screen protector" } } },
{$sort: { score: { $meta: "textScore" } }},
{$skip: 0 },
{$limit: 10}
])

I have also text index for product_title and 1 (ask) index for category_name

I am also thinking how to select a particular collection like “Phones” and do search only there if user provides it

Would be grateful for any help!

Hi @Denys_Medvediev, could you please share your sample document and index fields?

By given information, I can suggest you to please use and specify the index name and path too while searching for data in this way y can get the results much faster.