-
Type: New Feature
-
Resolution: Duplicate
-
Priority: Major - P3
-
None
-
Affects Version/s: None
-
Component/s: Index Maintenance
(copied from http://groups.google.com/group/mongodb-user/browse_thread/thread/8d6f4fe174895fda)
Given a collection like this:
db.player = {
name: "Glenn",
scores: {
ping_pong:
,
golf:
}
}
I want an index on each of the keys of scores, so I can efficiently eg. find and sort on 'scores.ping_pong.points'. However, there are too many keys to create indexes on 'scores.ping_pong', 'scores.golf', and so on--there many be hundreds of possible keys.
It would help if it was possible to create an index on 'scores.$key.points', which would effectively be a compound index on (key name, points), so this type of structure can be indexed without creating an index for every possible key. That way, a single index would work for both find(
{scores.ping_pong.points: 100}) and find(
{scores.golf.points: 100}). In the above data, two index entries would be created: one on ('ping_pong', 2000) and the other on ('golf', 100).
This could probably also be used to accelerate $exists. A (
{'name': 1, 'scores.$key': 1}) index should allow find({name: 'Glenn', 'scores.ping_pong': {$exists: true}}) to be done efficiently.
A compound key of ('x', 'scores.$key.points', 'y') would expand to ('x', 'key name', 'points', 'y'). Similarly, ('a.$key.b', 'b.$key.c') expands to ('first key name', 'b', 'second key name', 'c').