0

我的产品集合中有 3 个文档:

samsung = {
  title: 'Samsung Galaxy S III',
  category_id: ObjectId("50bcc2f0b910a6c1936a4424"),
  properties: [
    {
      title: 'OS',
      value: 'Android'
    },
    {
      title: 'Display',
      value: '4.8"'
    }
  ]
}

htc = {
  title: 'HTC One X',
  category_id: ObjectId("50bcc2f0b910a6c1936a4424"),
  properties: [
    {
      title: 'OS',
      value: 'Android'
    },
    {
      title: 'Display',
      value: '4.7"'
    }
  ]
}

apple = {
  title: 'Apple iPhone 5',
  category_id: ObjectId("50bcc2f0b910a6c1936a4424"),
  properties: [
    {
      title: 'OS',
      value: 'iOS'
    },
    {
      title: 'Display',
      value: '4"'
    }
  ]
}

和索引{category_id: 1, 'properties.title': 1, 'properties.value': 1}

我认为索引应该是这样的:

ObjectId("50bcc2f0b910a6c1936a4424")
  OS
    Android
      samsung
      htc
    iOS
      apple
  Display
    4.8"
      samsung
    4.7"
      htc
    4"
      apple

我期望该查询:

{
  category_id: ObjectId("50bcc2f0b910a6c1936a4424"),
  properties: {
    $elemMatch: {
      title: 'OS',
      value: 'Android'
    }
  }
}

nscanned == 2,nscannedObjects == 2,n == 2。但我得到这样的输出explain()

{
        "cursor" : "BtreeCursor category_id_1_properties.title_1_properties.value_1",
        "nscanned" : 3,
        "nscannedObjects" : 3,
        "n" : 2,
        "millis" : 0,
        "nYields" : 0,
        "nChunkSkips" : 0,
        "isMultiKey" : true,
        "indexOnly" : false,
        "indexBounds" : {
                "category_id" : [
                        [
                                ObjectId("50bcc2f0b910a6c1936a4424"),
                                ObjectId("50bcc2f0b910a6c1936a4424")
                        ]
                ],
                "properties.title" : [
                        [
                                "OS",
                                "OS"
                        ]
                ],
                "properties.value" : [
                        [
                                {
                                        "$minElement" : 1
                                },
                                {
                                        "$maxElement" : 1
                                }
                        ]
                ]
        }
}

你能解释一下为什么indexBounds不是"properties.value"["Android","Android"]

我可以重写查询或重建索引以使用索引获得正确的手机吗?

4

1 回答 1

1

这是 MongoDB 的一个错误:https ://jira.mongodb.org/browse/SERVER-3104 。

开发版本 2.3.1 有相同的错误,但在最新的夜间版本(我在 2012-12-03 测试过)中,一切似乎都运行良好。

于 2012-12-03T18:04:07.277 回答