MongoDB 高级索引

  • 高级索引

    我们已将以下文档插入名为用户的集合中,如下所示-
    
    db.users.insert(
      {
        "address": {
          "city": "Los Angeles",
          "state": "California",
          "pincode": "123"
        },
        "tags": [
          "music",
          "cricket",
          "blogs"
        ],
        "name": "Tom Benzamin"
      }
    )
    
    上面的文档包含一个address子文档和一个tags数组。
  • 索引数组字段

    假设我们要根据用户的标签搜索用户文档。为此,我们将在集合中的标签数组上创建索引。依次在数组上创建索引将为其每个字段创建单独的索引条目。因此,在本例中,当我们在标签数组上创建索引时,将为其音乐,板球和博客的值创建单独的索引。要在标签数组上创建索引,请使用以下代码-
    
    >db.users.createIndex({"tags":1})
    {
    "createdCollectionAutomatically" : false,
    "numIndexesBefore" : 2,
    "numIndexesAfter" : 3,
    "ok" : 1
    }
    >
    
    创建索引后,我们可以像这样搜索集合的标签字段-
    
    > db.users.find({tags:"cricket"}).pretty()
    { 
        "_id" : ObjectId("5f4f079c40ea6da6414f2bd1"), 
        "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.0, 
            "namespace" : "test.users", 
            "indexFilterSet" : false, 
            "parsedQuery" : {
                "tags" : {
                    "$eq" : "cricket"
                }
            }, 
            "queryHash" : "9D3B61A7", 
            "planCacheKey" : "04C9997B", 
            "winningPlan" : {
                "stage" : "FETCH", 
                "inputStage" : {
                    "stage" : "IXSCAN", 
                    "keyPattern" : {
                        "tags" : 1.0
                    }, 
                    "indexName" : "tags_1", 
                    "isMultiKey" : true, 
                    "multiKeyPaths" : {
                        "tags" : [
                            "tags"
                        ]
                    }, 
                    "isUnique" : false, 
                    "isSparse" : false, 
                    "isPartial" : false, 
                    "indexVersion" : 2.0, 
                    "direction" : "forward", 
                    "indexBounds" : {
                        "tags" : [
                            "[\"cricket\", \"cricket\"]"
                        ]
                    }
                }
            }, 
            "rejectedPlans" : [
    
            ]
        }, 
        "serverInfo" : {
            "host" : "www.jc2182.com", 
            "port" : 27017.0, 
            "version" : "4.4.0", 
            "gitVersion" : "563487e100c4215e2dce98d0af2a6a5a2d67c5cf"
        }, 
        "ok" : 1.0
    }
    
  • 索引子文档字段

    假设我们要根据城市,州和密码字段搜索文档。由于所有这些字段都是地址子文档字段的一部分,因此我们将在子文档的所有字段上创建索引。要在子文档的所有三个字段上创建索引,请使用以下代码-
    
    >db.users.createIndex({"address.city":1,"address.state":1,"address.pincode":1})
    { 
        "createdCollectionAutomatically" : false, 
        "numIndexesBefore" : 2.0, 
        "numIndexesAfter" : 3.0, 
        "ok" : 1.0
    }
    
    创建索引后,我们可以使用该索引搜索任何子文档字段,如下所示:
    
    > db.users.find({"address.city":"Los Angeles"}).pretty()
    { 
        "_id" : ObjectId("5f4f079c40ea6da6414f2bd1"), 
        "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("5f4f079c40ea6da6414f2bd1"), 
        "address" : {
            "city" : "Los Angeles", 
            "state" : "California", 
            "pincode" : "123"
        }, 
        "tags" : [
            "music", 
            "cricket", 
            "blogs"
        ], 
        "name" : "Tom Benzamin"
    }