MongoDB 高级索引

我们在名为 users 的集合中插入了以下文档,如下所示

db.users.insert(
    {
        "address": {
            "city": "Los Angeles",
            "state": "California",
            "pincode": "123"
        },
        "tags": [
            "music",
            "cricket",
            "blogs"
        ],
        "name": "Tom Benzamin"
    }
)

以上文档包含了 address 子文档和 tags 数组。


索引数组字段

假设我们要根据用户的标签搜索用户文档。为此,我们要在集合中的标签数组上创建一个索引。

在数组上创建索引依次为其每个字段创建单独的索引条目。因此,在我们的例子中,当我们在标签数组上创建索引时,将为其值 music、cricket 和 blog 创建单独的索引。

可以使用以下代码,在标签数组上创建索引。

>db.users.createIndex({"tags":1})
{
"createdCollectionAutomatically" : false,
"numIndexesBefore" : 2,
"numIndexesAfter" : 3,
"ok" : 1
}
>

创建索引后,我们可以这样检索集合的 tags 字段:

> db.users.find({tags:"cricket"}).pretty()
{
    "_id" : ObjectId("5dd7c927f1dd4583e7103fdf"),
    "address" : {
        "city" : "Los Angeles",
        "state" : "California",
        "pincode" : "123"
    },
    "tags" : [
        "music",
        "cricket",
        "blogs"
    ],
    "name" : "Tom Benzamin"
}

要验证是否使用了正确的索引,使用以下explain命令

>db.users.find({tags:"cricket"}).explain()

将输出以下结果:

{
    "queryPlanner" : {
        "plannerVersion" : 1,
        "namespace" : "mydb.users",
        "indexFilterSet" : false,
        "parsedQuery" : {
            "tags" : {
                "$eq" : "cricket"
            }
        },
        "queryHash" : "9D3B61A7",
        "planCacheKey" : "04C9997B",
        "winningPlan" : {
            "stage" : "FETCH",
            "inputStage" : {
                "stage" : "IXSCAN",
                "keyPattern" : {
                    "tags" : 1
                },
                "indexName" : "tags_1",
                "isMultiKey" : false,
                "multiKeyPaths" : {
                    "tags" : [ ]
                },
                "isUnique" : false,
                "isSparse" : false,
                "isPartial" : false,
                "indexVersion" : 2,
                "direction" : "forward",
                "indexBounds" : {
                    "tags" : [
                        "[\"cricket\", \"cricket\"]"
                    ]
                }
            }
        },
        "rejectedPlans" : [ ]
    },
    "serverInfo" : {
        "host" : "Krishna",
        "port" : 27017,
        "version" : "4.2.1",
        "gitVersion" : "edf6d45851c0b9ee15548f0f847df141764a317e"
    },
    "ok" : 1
}

以上命令执行结果中显示 "cursor" : "BtreeCursor tags_1" ,则表示已经使用了索引。


索引子文档字段

假设我们需要通过city、state、pincode字段来检索文档,由于这些字段是子文档的字段,所以我们需要对子文档建立索引。

为子文档的三个字段创建索引,命令如下:

>db.users.createIndex({"address.city":1,"address.state":1,"address.pincode":1})
{
    "numIndexesBefore" : 4,
    "numIndexesAfter" : 4,
    "note" : "all indexes already exist",
    "ok" : 1
}

创建索引后,我们可以使用此索引搜索任何子文档字段,如下所示

> db.users.find({"address.city":"Los Angeles"}).pretty()
{
    "_id" : ObjectId("5dd7c927f1dd4583e7103fdf"),
    "address" : {
        "city" : "Los Angeles",
        "state" : "California",
        "pincode" : "123"
    },
    "tags" : [
        "music",
        "cricket",
        "blogs"
    ],
    "name" : "Tom Benzamin"
}

请记住,查询表达式必须遵循指定索引的顺序。所以上面创建的索引将支持以下查询

> db.users.find({"address.city":"Los Angeles","address.state":"California"}).pretty()
{
    "_id" : ObjectId("5dd7c927f1dd4583e7103fdf"),
    "address" : {
        "city" : "Los Angeles",
        "state" : "California",
        "pincode" : "123"
    },
    "tags" : [
        "music",
        "cricket",
        "blogs"
    ],
    "name" : "Tom Benzamin"
}

查看笔记

扫码一下
查看教程更方便