Ensuring an index on a element of an array does not work as expected for me.
E.g. with the following data
> db.test.find()
{ "_id" : 7891236423, "arr" : [ "a", "b", "c" ] }
{ "_id" : 7891236424, "arr" : [ "b", "c", "d" ] }
{ "_id" : 7891236425, "arr" : [ "b", "c", "a" ] }
>
the following query works as expected when there is no index
> db.test.find({"arr.0":"a"})
{ "_id" : 7891236423, "arr" : [ "a", "b", "c" ] }
>
but the same query does not return a result if I try to create an index on arr.0
> db.test.ensureIndex({"arr.0":1})
> db.test.find({"arr.0":"a"})
>
I do not understand what is happening here, in my opinion an index should never have an effect on the returned data. Or should it?
Is this a bug? Or index abuse? I'd be happy for any explanations for this behavior, and even happier for any hints how I can achieve an index on a single array element.
PS: I know that I can create an index on arr
and query arr
first before querying arr.0
, which is a valid workaround for the given example. But this does not help as soon as I need to sort a huge amount of data by arr.0
(MongoDB demands an index for this)
This is a known problem with the v0 indexes used in 1.8 and prior. The new v1 indexes in 2.0+ support this. Upgrading to 2.0.3 should solve your problem. You can find instructions on upgrading to 2.0 here: http://www.mongodb.org/display/DOCS/2.0+Release+Notes#2.0ReleaseNotes-Upgrading
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With