What is the proper way to ensure mindodb indexIndex indexing on a multidimensional array?

I have data in mongodb something like this. There is a collection of cats. Cats are sorted into different categories and ranked from 1 to 100. One cat can be in 2 or more categories. There are 1000 categories.

COLLECTION : "cats"

KEYS

rank.category1 = 1; // ranked 1st in category #1 rank.category2 = 13; // ranked 13th in category #2 rank.category425 = 50; // ranked 50th in category #425 

Question : If I want to do find () to return all the "cats" that have a "rank" in category 2, where $ exists => "rank.category2", what is the correct way to index this? Can I just put a simple ascending index into the "rank" collection or do I need an index for all 1000+ categories * of keys? Is there a better way to store this information or an easier way to index it?

+6
source share
1 answer

What about...

 rank.categories = [1, 2, 425]; rank.category = { 1 : 1, 2 : 13, 425 : 50 } 

You can index db.collection.ensureIndex({"categories":1}) . Now you can search for categories and get a rating for everyone when you find it.

+7
source

Source: https://habr.com/ru/post/1014199/


All Articles