0

I currently have a assignments collection with the following structure:

{
  _id:Object("basdfasdfasdfasdf"),
  name:"Entry one",
  ...//other properties
  services:[
    {
      ...//other properties
      workers: [
        {
           name:"John"
        }
      ]
    }
  ]
}

Am I able to create a multikey index on workers.name array? This is an array of arrays situation. If this isn't possible I would need to make the services to be it's own collection so we can put a multikey index on the array of service.workers, correct? The reason I don't want services to be it's own collection is so that I wouldn't need to do an aggregation step everytime I want to pull it out of the db.

Thoughts?

Jose
  • 10,891
  • 19
  • 67
  • 89
  • 2
    Yes, you can. `db.collection.createIndex({ 'services.workers.name': 1 })` – Alex Blex Jan 09 '23 at 21:50
  • @AlexBlex, thanks for the insight. i guess I'll do a test db and add some data and ensure that the multikey index does cover the query. Do you know if there is a limit to the amount of depth a multi-key index can go? – Jose Jan 09 '23 at 22:14
  • No, only standard limit of up to 100 levels for documents. You just cannot insert documents that exceed the limit, so no way to index them. It just picks all values from the dot-path and add them to the b-tree referencing to the source documents. – Alex Blex Jan 09 '23 at 22:25

0 Answers0