Hello Jack,
As Aaron recently pointed out in the server ticket you reference
above, you will be able to use $elemMatch to restrict the index bounds
for range queries using multi-key indexes:
https://jira.mongodb.org/browse/SERVER-4180.
On Aug 20, 2:55 pm, Jack Xu <
jack...@gmail.com> wrote:
> in mongod 2.0+, when I run the following query in a single element multikey
> indexed document, it works fine and leverages multikey index:
>
> *db.objects.drop();
>
> > **db.objects.save({attrib : [ {d : 3}]})
> > **db.objects.save({attrib : [ {d : 5} ]})
> > **db.objects.save({attrib : [ {d : 7}]})
> > **db.objects.ensureIndex({attrib : 1})
> > **db.objects.find({attrib: {$gt: {d: 4}, $lt: {d: 6}}}).explain();*
>
> {
> "cursor" : "BtreeCursor attrib_1",
> "isMultiKey" : false,
> "n" : 1,
> "nscannedObjects" : 1,
> "nscanned" : 1,
> "nscannedObjectsAllPlans" : 1,
> "nscannedAllPlans" : 1,
> "scanAndOrder" : false,
> "indexOnly" : false,
> "nYields" : 0,
> "nChunkSkips" : 0,
> "millis" : 0,
> "indexBounds" : {
> "attrib" : [
> [
> {
> "d" : 4},
>
> {
> "d" : 6}
>
> ]
> ]},
>
> "server" : "lucid64:27017"
>
> }
>
> *However, as soon as there are multiple elements in the array, one of the
> bounds is simply ignored! *
>
> *db.objects.drop();
>
> > **db.objects.save({attrib : [ {c : 'test'} , {d : 3} ]})
> > **db.objects.save({attrib : [ {c : 'test1'}, {d : 5}]})
> > **db.objects.save({attrib : [ {c : 'test3'}, {d : 7} ]})
> > **db.objects.ensureIndex({attrib : 1})
> > **db.objects.find({attrib: {$gt: {d: 4}, $lt: {d: 6}}}).explain();*
>
> *
> *
> *
> {
> "cursor" : "BtreeCursor attrib_1",
> "isMultiKey" : true,
> "n" : 2,
> "nscannedObjects" : 5,
> "nscanned" : 5,
> "nscannedObjectsAllPlans" : 5,
> "nscannedAllPlans" : 5,
> "scanAndOrder" : false,
> "indexOnly" : false,
> "nYields" : 0,
> "nChunkSkips" : 0,
> "millis" : 0,
> "indexBounds" : {
> "attrib" : [
> [
> {
> "d" : 4},
>
> {
> "$maxElement" : 1}
>
> ]
> ]},
>
> "server" : "lucid64:27017"
>
> }
>
> I do understand this issue has been reported inhttps://
jira.mongodb.org/browse/SERVER-4155, but the answer is hard for me
> to understand.
>
> also, btw, both scenarios work perfectly in 1.8.6.
>
> please help.
>
> *