If every document has an array of objects, let say :
hobbies:[
{
"title": "Swimming",
"frequency": 4
},
{
"title": "Playing",
"frequency": 3
}
]
and I use hobbies as an Index, then how all the documents in my db will be stored in an sorted manner? Which field will it consider to sort all the documents in index?
You can create an index on
hobbiesfield like this as a compound-index :So as
hobbiesis an array then eventually if you gethobbies.title,hobbies.frequencyit will also be an array, So as if MongoDB finds an array to be indexed then it would create multikey-index on that particular field (Basically in above scenario your document will be unwinded into two docs ontitle & frequencyone for first object in array & another for second object in array in ascending order).