for example I have collection foo of documents like that:
{"tag_cloud":[{"value":"games", "count":10}, {"value":"girls", "count":500}]}
{"tag_cloud":[{"value":"games", "count":5}, {"value":"stack", "count":100}]}
{"tag_cloud":[{"value":"mongodb", "count":1000}, {"value":"thread", "count":15}]}
what difference between two types of index:
- ensureIndex({“tag_cloud”})
- ensureIndex({“tag_cloud.value”}); ensureIndex({“tag_cloud.count”})
in context of request:
db.foo.find({"tag_cloud.value":"games"}).sort({"tag_cloud.count":-1});
Thank you!!!
MongoDB can only use one index at a time per query, so your suggestion of creating two indexes will not work. Instead, I would create a compound index:
This index can be used both to filter to only the particular element or elements you want to consider, and then also return in descending sorted order: