MondogDB Create index for nested array

68 Views Asked by At

I have this object

{
"sales": [
   [
     {
       "id": "123"
     }
   ]
]
}

May i add an index on the "id" field? How?

1

There are 1 best solutions below

0
We do the best for You On

{ "sales": [ [ { "id": "123" } ] ] }

Firstly, This case is about "indexing nested arrays". One minor observation is that the outer array does not have a key. Is it intentional ? If intentional, is there any chance of reviewing the schema. The following 2 points would provide more about it.

{ "sales": [ x: [ { "id": "123" } ] ] }

Secondly, the above schema means x is a nested array of the array "sales". Is this the kind of schema you are looking for ?. If so, a compound index "sales.x.id" would normally be supported.

Thirdly, you may be able to see a similar case handled in the following link. MongoDB query optimisation for nested array of objects

Thank you WeDoTheBest4You