Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

264
Views
Whats the proper way to ensureIndex index mongodb field on a multidimensional array?

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?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!