1

I have collection of objects which have multiple array fields. Something like:

{
    sponsors: {
      lead_sponsor: {
         name: "Mark",
         type: "Individual"
      }
      collaborators: [
         {name: "Company Ltd.", type: "Company"},
         {name: "Industry Company Ltd.", type: "Company"}
      ]
    },
    terms: ["Some term", "Some term 2", "Some term 3"],
    keywords: ["word1", "word2", "word3", "word4"],
    description: "Here might be big text with detailed description",
    status: "Active",
}

How can I optimize query like next one?

{
  status: "Active", 
  $or: [
    {"lead_sponsor.name": /company/i}, 
    {"collaborators.name": /company/i},
    {"terms": /phrase/i},
    {"keywords": /phrase/i},
    {"description": /phrase/i},
  ]
}

Compound indexes cannot have more then one array field. Will it help if I copy all data from array fields into new field searchArray and will add compound index that includes only this array? Is it good approach?

Rayz
  • 523
  • 1
  • 4
  • 19
  • 2
    Yes. use `searchArray` with a structre like `"searchArray": [{ "type": "keword", "content": "word1" },{ "type": "term", "content": "Some term" }]`. Then you can consistently index on even "both" the `"type"` and `"content"` properties without breaking the rules of multikey indexes. With two arrays, the possible combinations just blows out of proportion. You just about always really mean a single array with dicerned structure anyway. – Blakes Seven Apr 19 '16 at 10:44

0 Answers0