I have data in mongodb something like so. There is a collection of cats. The cats are sorted into different categories and ranked 1 through 100. One cat might be located in 2 or more categories. There are 1000s of 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 a find() to return all "cats" that have a "rank" in "category2" where $exists => "rank.category2" what is the proper way to index this? Can I just put a simple ascending index on "rank" collection or do I need an index on all 1000+ category* keys? Is there a better way to store this information or an easier way to index it?
How 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 through categories and get ranking of each when you find one.