0

I have structure like below

{
  "name": "",
  "age": 35,
  "hobbies": "gardening, skiing, horse riding, soccer",
  "Address": {
    "addrline 1": "",
    "addrline 2": "",
    "City": "",
    "Zip": ""
  }
}

Would like to search based on hobby. e.g. for input "skiing" should return this Document. I have around 80k records. What would be the best Indexing strategy for "hobbies" element so that it can return result fast.

Do I have to search "hobbies" only by string based regex pattern or is there any other technique for faster data retrieval?

Debopam
  • 3,198
  • 6
  • 41
  • 72

1 Answers1

5

A better design would be to store hobbies as an array

 "hobbies": ["gardening", "skiing", "horse riding", "soccer"]

Then , you can use $in query to match any one or multiple hobbies. Create an index on the hobbies key for faster querying.

DhruvPathak
  • 42,059
  • 16
  • 116
  • 175
  • Totally agree with @DhruvPathak , maybe you can run a script or command and turn the comma separated value to an array, Then add a multikey index on hobbies, that way you won't have to do a regex query, regex are not efficient in using indexes for faster search. – Rahul Kumar Sep 19 '16 at 18:49