【问题标题】:MongoDB multikey indexes for array of attributes属性数组的 MongoDB 多键索引
【发布时间】:2012-12-03 16:05:01
【问题描述】:

我的产品集合中有 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
                                }
                        ]
                ]
        }
}

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

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

【问题讨论】:

    标签: mongodb indexing


    【解决方案1】:

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

    开发版本 2.3.1 有同样的错误,但在最新的夜间版本(我在 2012 年 12 月 3 日测试过)中一切正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-10-08
      • 2014-02-18
      • 2013-09-05
      • 2015-03-26
      • 1970-01-01
      • 2011-11-15
      • 1970-01-01
      相关资源
      最近更新 更多