MongoDB多键索引用于属性数组

时间:2012-12-03 16:05:01

标签: mongodb indexing

我的产品系列中有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"] {{1}}为何不是{{1}}的原因?

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

1 个答案:

答案 0 :(得分:1)

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

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