Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MongoDB是否使用索引组合来缩小要扫描的文档?_Mongodb_Search_Indexing - Fatal编程技术网

MongoDB是否使用索引组合来缩小要扫描的文档?

MongoDB是否使用索引组合来缩小要扫描的文档?,mongodb,search,indexing,Mongodb,Search,Indexing,我知道索引交叉点,但从文档中我不清楚以下场景是否有效: 我的文档彼此非常不同,都在一个集合中。每个文档模式都有一个或多个具有不同值的实例,可能有数十万个值。我使用散列值来标识记录模式,然后使用其他字段来搜索特定文档 例如,我在集合中得到了以下条目: { type: "ABC", details: { model: "14Q3", manufacturer: "M1 Corporation" }, stock: [ { size: "M", qty: 50 } ],

我知道索引交叉点,但从文档中我不清楚以下场景是否有效:

我的文档彼此非常不同,都在一个集合中。每个文档模式都有一个或多个具有不同值的实例,可能有数十万个值。我使用散列值来标识记录模式,然后使用其他字段来搜索特定文档

例如,我在集合中得到了以下条目:

 {
    type: "ABC",
    details: { model: "14Q3", manufacturer: "M1 Corporation" },
    stock: [ { size: "M", qty: 50 } ],
    category: "clothing"
  },
  {
    item: "ABC",
    details: { model: "14Q3", manufacturer: "ABC Company" },
    stock: [ { size: "S", qty: 5 }, { size: "M", qty: 5 }, { size: "L", qty: 1 } ],
    category: "clothing"
  },
  {
    type: "XYZ",
    message: "a generic text message",
    date: [ "tag1", "tag2" , "tag3" ],
    category: "houseware",
    age: 12,
  }
  ...
> db.test.find()
{ "_id" : ObjectId("549d0b5bde6b4daa2e1f1859"), "name" : "Adam", "age" : 15 }
{ "_id" : ObjectId("549d0b70de6b4daa2e1f185a"), "name" : "John", "hobby" : "skiing" }
{ "_id" : ObjectId("549d0b96de6b4daa2e1f185b"), "name" : "Barbara", "occupation" : "student" }
查询时,我计划使用该类型缩小要搜索的文档范围,然后使用其他索引字段进行搜索

MongoDB是否能够利用覆盖我搜索的字段的不同索引来执行这些查询

另外,如果我在搜索中包括索引类型字段和非索引字段,它会扫描所有文档还是利用类型字段索引来缩小范围,然后才扫描这些文档以获取其他条件

我在MongoDB 2.6.6上


谢谢

是和否。它不会使用索引组合,但您可以创建复合索引

要简化,例如,如果您将有一个集合:

 {
    type: "ABC",
    details: { model: "14Q3", manufacturer: "M1 Corporation" },
    stock: [ { size: "M", qty: 50 } ],
    category: "clothing"
  },
  {
    item: "ABC",
    details: { model: "14Q3", manufacturer: "ABC Company" },
    stock: [ { size: "S", qty: 5 }, { size: "M", qty: 5 }, { size: "L", qty: 1 } ],
    category: "clothing"
  },
  {
    type: "XYZ",
    message: "a generic text message",
    date: [ "tag1", "tag2" , "tag3" ],
    category: "houseware",
    age: 12,
  }
  ...
> db.test.find()
{ "_id" : ObjectId("549d0b5bde6b4daa2e1f1859"), "name" : "Adam", "age" : 15 }
{ "_id" : ObjectId("549d0b70de6b4daa2e1f185a"), "name" : "John", "hobby" : "skiing" }
{ "_id" : ObjectId("549d0b96de6b4daa2e1f185b"), "name" : "Barbara", "occupation" : "student" }
然后使用名称和每个特定值创建复合索引:

db.test.ensureIndex({name: 1, age: 1})
db.test.ensureIndex({name: 1, hobby: 1})
db.test.ensureIndex({name: 1, occupation: 1})
然后您将尝试查找特定文档:

> db.test.find({name: "Adam", age: 15}).explain()
{
    "cursor" : "BtreeCursor name_1_age_1",
    "isMultiKey" : false,
    "n" : 1,
    "nscannedObjects" : 1,
    "nscanned" : 1,
    "nscannedObjectsAllPlans" : 2,
    "nscannedAllPlans" : 2,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 0,
    "nChunkSkips" : 0,
    "millis" : 0,
    "indexBounds" : {
        "name" : [
            [
                "Adam",
                "Adam"
            ]
        ],
        "age" : [
            [
                15,
                15
            ]
        ]
    },
    "server" : "CBS:27017",
    "filterSet" : false
}
您将看到,它使用了正确的索引,并且只扫描了一个文档来查找它。 使用explain命令检查每个查询使用的索引、搜索文档和搜索时间。这对理解mongodb有很大帮助

有关索引的详细信息: 并解释:

您还可以设置稀疏索引,该索引将只引用包含特定字段的文件,但也没有更多差异。有关稀疏索引的详细信息: