0

We have a very big collection, with several indexes.

Since an index is basically a table with the indexed field as key, and a list of ObjectIDs as value, we were wondering if we could somehow get the key that has the highest number of Objects it points to.

For example, if we have a collection:

{ _id: 1, a : 1, b : 1 },
{ _id: 2, a : 2, b : 2 },
{ _id: 3, a : 2, b : 3 },
{ _id: 4, a : 2, b : 4 },
{ _id: 5, a : 3, b : 4 },
{ _id: 6, a : 3, b : 4 },
{ _id: 7, a : 4, b : 4 }

Where there's an index of "a". I assume there a table somewhere that looks like this:

index a:

"1" => [ 1 ], 
"2" => [ 2, 3, 4 ],
"3" => [ 5, 6 ],
"4" => [ 7 ]

In which case we'd like to somehow query for the index value with the longest list of objects - "2".

Is something like that possible in MongoDB?

marmor
  • 27,641
  • 11
  • 107
  • 150

1 Answers1

0

The answer is no.

Actually, indexes don't have a list of document objectId's, just pointers to the data. To get document's objectId, system must go to the disk to read it. That's why, if you have a index what can answer to your query and you don't need _id in your result, remember always project {_id:0, key1:1, key2:1, keyX:1} so result can be returned from index and there is no need to go to the disk.

JJussi
  • 1,540
  • 12
  • 12