我的编程空间,编程开发者的网络收藏夹
学习永远不晚

MongoDB中索引怎么用

短信预约 -IT技能 免费直播动态提醒
省份

北京

  • 北京
  • 上海
  • 天津
  • 重庆
  • 河北
  • 山东
  • 辽宁
  • 黑龙江
  • 吉林
  • 甘肃
  • 青海
  • 河南
  • 江苏
  • 湖北
  • 湖南
  • 江西
  • 浙江
  • 广东
  • 云南
  • 福建
  • 海南
  • 山西
  • 四川
  • 陕西
  • 贵州
  • 安徽
  • 广西
  • 内蒙
  • 西藏
  • 新疆
  • 宁夏
  • 兵团
手机号立即预约

请填写图片验证码后获取短信验证码

看不清楚,换张图片

免费获取短信验证码

MongoDB中索引怎么用

小编给大家分享一下MongoDB中索引怎么用,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

在任何数据库之中,索引都是一种提升数据库检索性能的手段。
MongoDB数据库里面依然会存在两种索引创建:一种是自动创建,另外一种是手工创建。

范例:重新准备一个新的简单集合。
db.emp.drop();
db.emp.insert({"name":"赵一","sex":"男","age":30,"sal":1000,"loc":"北京"});
db.emp.insert({"name":"钱二","sex":"女","age":22,"sal":5000,"loc":"上海"});
db.emp.insert({"name":"孙三","sex":"男","age":40,"sal":2000,"loc":"深圳"});
db.emp.insert({"name":"李四","sex":"女","age":30,"sal":7000,"loc":"北京"});
db.emp.insert({"name":"周五","sex":"女","age":30,"sal":6400,"loc":"北京"});
db.emp.insert({"name":"吴六","sex":"男","age":30,"sal":2500,"loc":"重庆"});
db.emp.insert({"name":"郑七","sex":"女","age":50,"sal":4700,"loc":"成都"});
db.emp.insert({"name":"王八","sex":"男","age":35,"sal":8000,"loc":"北京"});

此时在emp的集合上并没有去设置任何的索引,可以通过getIndexes()函数来观察在emp集合中的索引。

范例:查询默认状态下emp集合的索引内容
> db.emp.getIndexes();
[
        {
                "v" : 2,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "hr.emp"
        }
]

现在发现会存在有一个“_id”列的索引内容。但是如果要想创建自己的索引,则可以使用如下的语法:
索引创建:db.集合名称.createIndex({列:1})
设置的1表示索引按照升序的方式进行排序,如果使用降序则使用“-1”

范例:创建一个索引,在age字段上设置一个降序索引
> db.emp.createIndex({"age":-1})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
此时并没有设置索引的名字,所以名字是自动命名的。命名的规范:“字段名称_索引的排序模式”
> db.emp.getIndexes();
[
        {
                "v" : 2,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "hr.emp"
        },
        {
                "v" : 2,
                "key" : {
                        "age" : -1
                },
                "name" : "age_-1",
                "ns" : "hr.emp"
        }
]

范例:针对于当前的age字段上的索引做一个分析
> db.emp.find({"age":30}).explain("executionStats");
{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "hr.emp",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "age" : {
                                "$eq" : 30
                        }
                },
                "winningPlan" : {
                        "stage" : "FETCH",
                        "inputStage" : {
                                "stage" : "IXSCAN",
                                "keyPattern" : {
                                        "age" : -1
                                },
                                "indexName" : "age_-1",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                        "age" : [ ]
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "forward",
                                "indexBounds" : {
                                        "age" : [
                                                "[30.0, 30.0]"
                                        ]
                                }
                        }
                },
                "rejectedPlans" : [ ]
        },
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 4,
                "executionTimeMillis" : 0,
                "totalKeysExamined" : 4,
                "totalDocsExamined" : 4,
                "executionStages" : {
                        "stage" : "FETCH",
                        "nReturned" : 4,
                        "executionTimeMillisEstimate" : 0,
                        "works" : 5,
                        "advanced" : 4,
                        "needTime" : 0,
                        "needYield" : 0,
                        "saveState" : 0,
                        "restoreState" : 0,
                        "isEOF" : 1,
                        "invalidates" : 0,
                        "docsExamined" : 4,
                        "alreadyHasObj" : 0,
                        "inputStage" : {
                                "stage" : "IXSCAN",
                                "nReturned" : 4,
                                "executionTimeMillisEstimate" : 0,
                                "works" : 5,
                                "advanced" : 4,
                                "needTime" : 0,
                                "needYield" : 0,
                                "saveState" : 0,
                                "restoreState" : 0,
                                "isEOF" : 1,
                                "invalidates" : 0,
                                "keyPattern" : {
                                        "age" : -1
                                },
                                "indexName" : "age_-1",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                        "age" : [ ]
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "forward",
                                "indexBounds" : {
                                        "age" : [
                                                "[30.0, 30.0]"
                                        ]
                                },
                                "keysExamined" : 4,
                                "seeks" : 1,
                                "dupsTested" : 0,
                                "dupsDropped" : 0,
                                "seenInvalidated" : 0
                        }
                }
        },
        "serverInfo" : {
                "host" : "D2-LZY245",
                "port" : 27017,
                "version" : "3.4.7",
                "gitVersion" : "cf38c1b8a0a8dca4a11737581beafef4fe120bcd"
        },
        "ok" : 1
}

此时的查询使用了索引的技术,但是下面再来观察一个查询,不使用索引字段

范例:针对于sal字段进行查询
> db.emp.find({"sal":{"$gt":5000}}).explain("executionStats");
{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "hr.emp",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "sal" : {
                                "$gt" : 5000
                        }
                },
                "winningPlan" : {
                        "stage" : "COLLSCAN",
                        "filter" : {
                                "sal" : {
                                        "$gt" : 5000
                                }
                        },
                        "direction" : "forward"
                },
                "rejectedPlans" : [ ]
        },
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 3,
                "executionTimeMillis" : 0,
                "totalKeysExamined" : 0,
                "totalDocsExamined" : 8,
                "executionStages" : {
                        "stage" : "COLLSCAN",
                        "filter" : {
                                "sal" : {
                                        "$gt" : 5000
                                }
                        },
                        "nReturned" : 3,
                        "executionTimeMillisEstimate" : 0,
                        "works" : 10,
                        "advanced" : 3,
                        "needTime" : 6,
                        "needYield" : 0,
                        "saveState" : 0,
                        "restoreState" : 0,
                        "isEOF" : 1,
                        "invalidates" : 0,
                        "direction" : "forward",
                        "docsExamined" : 8
                }
        },
        "serverInfo" : {
                "host" : "D2-LZY245",
                "port" : 27017,
                "version" : "3.4.7",
                "gitVersion" : "cf38c1b8a0a8dca4a11737581beafef4fe120bcd"
        },
        "ok" : 1
}
此时在sal字段上并没有设置索引,所以当前的索引形式就变成了全集合扫描的模式。
但是如果说,现在换一种形式,年龄和薪水一起执行查询:
> db.emp.find({"age":30,"sal":7000}).explain("executionStats")
{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "hr.emp",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "$and" : [
                                {
                                        "age" : {
                                                "$eq" : 30
                                        }
                                },
                                {
                                        "sal" : {
                                                "$eq" : 7000
                                        }
                                }
                        ]
                },
                "winningPlan" : {
                        "stage" : "FETCH",
                        "filter" : {
                                "sal" : {
                                        "$eq" : 7000
                                }
                        },
                        "inputStage" : {
                                "stage" : "IXSCAN",
                                "keyPattern" : {
                                        "age" : -1
                                },
                                "indexName" : "age_-1",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                        "age" : [ ]
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "forward",
                                "indexBounds" : {
                                        "age" : [
                                                "[30.0, 30.0]"
                                        ]
                                }
                        }
                },
                "rejectedPlans" : [ ]
        },
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 1,
                "executionTimeMillis" : 0,
                "totalKeysExamined" : 4,
                "totalDocsExamined" : 4,
                "executionStages" : {
                        "stage" : "FETCH",
                        "filter" : {
                                "sal" : {
                                        "$eq" : 7000
                                }
                        },
                        "nReturned" : 1,
                        "executionTimeMillisEstimate" : 0,
                        "works" : 5,
                        "advanced" : 1,
                        "needTime" : 3,
                        "needYield" : 0,
                        "saveState" : 0,
                        "restoreState" : 0,
                        "isEOF" : 1,
                        "invalidates" : 0,
                        "docsExamined" : 4,
                        "alreadyHasObj" : 0,
                        "inputStage" : {
                                "stage" : "IXSCAN",
                                "nReturned" : 4,
                                "executionTimeMillisEstimate" : 0,
                                "works" : 5,
                                "advanced" : 4,
                                "needTime" : 0,
                                "needYield" : 0,
                                "saveState" : 0,
                                "restoreState" : 0,
                                "isEOF" : 1,
                                "invalidates" : 0,
                                "keyPattern" : {
                                        "age" : -1
                                },
                                "indexName" : "age_-1",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                        "age" : [ ]
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "forward",
                                "indexBounds" : {
                                        "age" : [
                                                "[30.0, 30.0]"
                                        ]
                                },
                                "keysExamined" : 4,
                                "seeks" : 1,
                                "dupsTested" : 0,
                                "dupsDropped" : 0,
                                "seenInvalidated" : 0
                        }
                }
        },
        "serverInfo" : {
                "host" : "D2-LZY245",
                "port" : 27017,
                "version" : "3.4.7",
                "gitVersion" : "cf38c1b8a0a8dca4a11737581beafef4fe120bcd"
        },
        "ok" : 1
}
这个时候虽然age字段上面存在索引,由于sal字段上面没有索引,所以依然使用的是全表扫描操作。为了解决此时的问题,可以使用一个复合索引。
> db.emp.createIndex({"age":-1,"sal":-1},{name:"age_-1_sal_-1_index"})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 2,
        "numIndexesAfter" : 3,
        "ok" : 1
}

范例:默认使用索引
> db.emp.find({"age":30,"sal":7000}).explain("executionStats")
{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "hr.emp",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "$and" : [
                                {
                                        "age" : {
                                                "$eq" : 30
                                        }
                                },
                                {
                                        "sal" : {
                                                "$eq" : 7000
                                        }
                                }
                        ]
                },
                "winningPlan" : {
                        "stage" : "FETCH",
                        "inputStage" : {
                                "stage" : "IXSCAN",
                                "keyPattern" : {
                                        "age" : -1,
                                        "sal" : -1
                                },
                                "indexName" : "age_-1_sal_-1_index",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                        "age" : [ ],
                                        "sal" : [ ]
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "forward",
                                "indexBounds" : {
                                        "age" : [
                                                "[30.0, 30.0]"
                                        ],
                                        "sal" : [
                                                "[7000.0, 7000.0]"
                                        ]
                                }
                        }
                },
                "rejectedPlans" : [
                        {
                                "stage" : "FETCH",
                                "filter" : {
                                        "sal" : {
                                                "$eq" : 7000
                                        }
                                },
                                "inputStage" : {
                                        "stage" : "IXSCAN",
                                        "keyPattern" : {
                                                "age" : -1
                                        },
                                        "indexName" : "age_-1",
                                        "isMultiKey" : false,
                                        "multiKeyPaths" : {
                                                "age" : [ ]
                                        },
                                        "isUnique" : false,
                                        "isSparse" : false,
                                        "isPartial" : false,
                                        "indexVersion" : 2,
                                        "direction" : "forward",
                                        "indexBounds" : {
                                                "age" : [
                                                        "[30.0, 30.0]"
                                                ]
                                        }
                                }
                        }
                ]
        },
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 1,
                "executionTimeMillis" : 0,
                "totalKeysExamined" : 1,
                "totalDocsExamined" : 1,
                "executionStages" : {
                        "stage" : "FETCH",
                        "nReturned" : 1,
                        "executionTimeMillisEstimate" : 0,
                        "works" : 3,
                        "advanced" : 1,
                        "needTime" : 0,
                        "needYield" : 0,
                        "saveState" : 0,
                        "restoreState" : 0,
                        "isEOF" : 1,
                        "invalidates" : 0,
                        "docsExamined" : 1,
                        "alreadyHasObj" : 0,
                        "inputStage" : {
                                "stage" : "IXSCAN",
                                "nReturned" : 1,
                                "executionTimeMillisEstimate" : 0,
                                "works" : 2,
                                "advanced" : 1,
                                "needTime" : 0,
                                "needYield" : 0,
                                "saveState" : 0,
                                "restoreState" : 0,
                                "isEOF" : 1,
                                "invalidates" : 0,
                                "keyPattern" : {
                                        "age" : -1,
                                        "sal" : -1
                                },
                                "indexName" : "age_-1_sal_-1_index",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                        "age" : [ ],
                                        "sal" : [ ]
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "forward",
                                "indexBounds" : {
                                        "age" : [
                                                "[30.0, 30.0]"
                                        ],
                                        "sal" : [
                                                "[7000.0, 7000.0]"
                                        ]
                                },
                                "keysExamined" : 1,
                                "seeks" : 1,
                                "dupsTested" : 0,
                                "dupsDropped" : 0,
                                "seenInvalidated" : 0
                        }
                }
        },
        "serverInfo" : {
                "host" : "D2-LZY245",
                "port" : 27017,
                "version" : "3.4.7",
                "gitVersion" : "cf38c1b8a0a8dca4a11737581beafef4fe120bcd"
        },
        "ok" : 1
}

但是如果换到了以下条件:
> db.emp.find({"$or":[{"age":{"$gt":30}},{"sal":{"$gt":5000}}]}).explain("executionStats")
{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "hr.emp",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "$or" : [
                                {
                                        "age" : {
                                                "$gt" : 30
                                        }
                                },
                                {
                                        "sal" : {
                                                "$gt" : 5000
                                        }
                                }
                        ]
                },
                "winningPlan" : {
                        "stage" : "SUBPLAN",
                        "inputStage" : {
                                "stage" : "COLLSCAN",
                                "filter" : {
                                        "$or" : [
                                                {
                                                        "age" : {
                                                                "$gt" : 30
                                                        }
                                                },
                                                {
                                                        "sal" : {
                                                                "$gt" : 5000
                                                        }
                                                }
                                        ]
                                },
                                "direction" : "forward"
                        }
                },
                "rejectedPlans" : [ ]
        },
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 5,
                "executionTimeMillis" : 0,
                "totalKeysExamined" : 0,
                "totalDocsExamined" : 8,
                "executionStages" : {
                        "stage" : "SUBPLAN",
                        "nReturned" : 5,
                        "executionTimeMillisEstimate" : 0,
                        "works" : 10,
                        "advanced" : 5,
                        "needTime" : 4,
                        "needYield" : 0,
                        "saveState" : 0,
                        "restoreState" : 0,
                        "isEOF" : 1,
                        "invalidates" : 0,
                        "inputStage" : {
                                "stage" : "COLLSCAN",
                                "filter" : {
                                        "$or" : [
                                                {
                                                        "age" : {
                                                                "$gt" : 30
                                                        }
                                                },
                                                {
                                                        "sal" : {
                                                                "$gt" : 5000
                                                        }
                                                }
                                        ]
                                },
                                "nReturned" : 5,
                                "executionTimeMillisEstimate" : 0,
                                "works" : 10,
                                "advanced" : 5,
                                "needTime" : 4,
                                "needYield" : 0,
                                "saveState" : 0,
                                "restoreState" : 0,
                                "isEOF" : 1,
                                "invalidates" : 0,
                                "direction" : "forward",
                                "docsExamined" : 8
                        }
                }
        },
        "serverInfo" : {
                "host" : "D2-LZY245",
                "port" : 27017,
                "version" : "3.4.7",
                "gitVersion" : "cf38c1b8a0a8dca4a11737581beafef4fe120bcd"
        },
        "ok" : 1
}

现在发现并没有使用索引,所以这个时候看能否强制使用一次索引:hint()函数为强制使用索引操作。
范例:强制使用索引
> db.emp.find({"$or":[{"age":{"$gt":50}},{"sal":{"$gt":3000}}]}).hint("age_-1_sal_-1_index").explain("executionStats")
{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "hr.emp",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "$or" : [
                                {
                                        "age" : {
                                                "$gt" : 50
                                        }
                                },
                                {
                                        "sal" : {
                                                "$gt" : 3000
                                        }
                                }
                        ]
                },
                "winningPlan" : {
                        "stage" : "FETCH",
                        "filter" : {
                                "$or" : [
                                        {
                                                "age" : {
                                                        "$gt" : 50
                                                }
                                        },
                                        {
                                                "sal" : {
                                                        "$gt" : 3000
                                                }
                                        }
                                ]
                        },
                        "inputStage" : {
                                "stage" : "IXSCAN",
                                "keyPattern" : {
                                        "age" : -1,
                                        "sal" : -1
                                },
                                "indexName" : "age_-1_sal_-1_index",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                        "age" : [ ],
                                        "sal" : [ ]
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "forward",
                                "indexBounds" : {
                                        "age" : [
                                                "[MaxKey, MinKey]"
                                        ],
                                        "sal" : [
                                                "[MaxKey, MinKey]"
                                        ]
                                }
                        }
                },
                "rejectedPlans" : [ ]
        },
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 5,
                "executionTimeMillis" : 0,
                "totalKeysExamined" : 8,
                "totalDocsExamined" : 8,
                "executionStages" : {
                        "stage" : "FETCH",
                        "filter" : {
                                "$or" : [
                                        {
                                                "age" : {
                                                        "$gt" : 50
                                                }
                                        },
                                        {
                                                "sal" : {
                                                        "$gt" : 3000
                                                }
                                        }
                                ]
                        },
                        "nReturned" : 5,
                        "executionTimeMillisEstimate" : 0,
                        "works" : 9,
                        "advanced" : 5,
                        "needTime" : 3,
                        "needYield" : 0,
                        "saveState" : 0,
                        "restoreState" : 0,
                        "isEOF" : 1,
                        "invalidates" : 0,
                        "docsExamined" : 8,
                        "alreadyHasObj" : 0,
                        "inputStage" : {
                                "stage" : "IXSCAN",
                                "nReturned" : 8,
                                "executionTimeMillisEstimate" : 0,
                                "works" : 9,
                                "advanced" : 8,
                                "needTime" : 0,
                                "needYield" : 0,
                                "saveState" : 0,
                                "restoreState" : 0,
                                "isEOF" : 1,
                                "invalidates" : 0,
                                "keyPattern" : {
                                        "age" : -1,
                                        "sal" : -1
                                },
                                "indexName" : "age_-1_sal_-1_index",
                                "isMultiKey" : false,
                                "multiKeyPaths" : {
                                        "age" : [ ],
                                        "sal" : [ ]
                                },
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 2,
                                "direction" : "forward",
                                "indexBounds" : {
                                        "age" : [
                                                "[MaxKey, MinKey]"
                                        ],
                                        "sal" : [
                                                "[MaxKey, MinKey]"
                                        ]
                                },
                                "keysExamined" : 8,
                                "seeks" : 1,
                                "dupsTested" : 0,
                                "dupsDropped" : 0,
                                "seenInvalidated" : 0
                        }
                }
        },
        "serverInfo" : {
                "host" : "D2-LZY245",
                "port" : 27017,
                "version" : "3.4.7",
                "gitVersion" : "cf38c1b8a0a8dca4a11737581beafef4fe120bcd"
        },
        "ok" : 1
}

如果正常来讲,这个代码根本就不可能调用默认的索引执行,如果需要可以使用hint()函数强制MongoDB必须使用索引。
如果在一个集合里面设置了过多的索引,实际上会导致性能下降。那么可以删除索引。

范例:删除一个索引
> db.emp.dropIndex("age_-1_sal_-1_index")
{ "nIndexesWas" : 3, "ok" : 1 }
> db.emp.getIndexes();
[
        {
                "v" : 2,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "hr.emp"
        },
        {
                "v" : 2,
                "key" : {
                        "age" : -1
                },
                "name" : "age_-1",
                "ns" : "hr.emp"
        }
]

范例:删除非"_id"的索引,也就是自定义的索引
> db.emp.dropIndexes();
{
        "nIndexesWas" : 2,
        "msg" : "non-_id indexes dropped for collection",
        "ok" : 1
}
> db.emp.getIndexes();
[
        {
                "v" : 2,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "hr.emp"
        }
]
以上是“MongoDB中索引怎么用”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

免责声明:

① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。

② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341

MongoDB中索引怎么用

下载Word文档到电脑,方便收藏和打印~

下载Word文档

猜你喜欢

mongodb怎么查看命中的索引

在MongoDB中,可以使用explain()方法来查看命中的索引。explain()方法会返回查询的执行计划,包括是否命中索引以及使用的索引信息。下面是使用explain()方法查看命中的索引的步骤:1. 打开MongoDB的命令行客户端
2023-08-30

MongoDB数据库索引怎么使用

这篇“MongoDB数据库索引怎么使用”文章的知识点大部分人都不太理解,所以小编给大家总结了以下内容,内容详细,步骤清晰,具有一定的借鉴价值,希望大家阅读完这篇文章能有所收获,下面我们一起来看看这篇“MongoDB数据库索引怎么使用”文章吧
2023-07-02

mongodb怎么建立hash索引

在MongoDB中,可以通过使用`createIndex()`方法来建立一个哈希索引。首先,连接到MongoDB数据库。然后,选择要创建哈希索引的集合。使用以下命令:```use your_database_name```接下来,使用`cr
2023-09-06

MongoDB中的索引是怎样的

本篇文章给大家分享的是有关MongoDB中的索引是怎样的,小编觉得挺实用的,因此分享给大家学习,希望大家阅读完这篇文章后可以有所收获,话不多说,跟着小编一起来看看吧。索引就像图书的目录一样,可以让我们快速定位到需要的内容,关系型数据库中有索
2023-06-19

mongodb怎么建立唯一索引

在MongoDB中,可以使用`createIndex()`方法来建立唯一索引。以下是建立唯一索引的步骤:1. 连接到MongoDB数据库。2. 选择要创建唯一索引的集合。3. 使用`createIndex()`方法来创建唯一索引,指定要创建
2023-08-23

MongoDB怎么让索引保持在内存中

MongoDB是一个高性能的数据库,它会自动将经常访问的数据和索引放入内存中以提高查询性能。然而,有时候我们需要强制将索引保持在内存中,以确保查询性能得到最大化。要让索引保持在内存中,可以使用以下方法:使用hint()方法:在查询中使用h
MongoDB怎么让索引保持在内存中
2024-04-19

mongodb怎么查询指定索引

在 MongoDB 中,可以使用 `db.collection.find()` 方法来查询指定索引。可以通过使用 `.hint()` 方法来指定索引。以下是一个使用指定索引查询的示例:```javascriptdb.collection.f
2023-09-04

mongodb怎么创建索引失败

当创建索引失败时,有以下几个可能的原因:1. 重复的索引名称:如果尝试创建重复名称的索引,MongoDB会报错。确保使用唯一的索引名称。2. 索引字段值过大:MongoDB的索引字段值的限制是16MB。如果尝试创建一个超过这个限制的索引,创
2023-08-31

MongoDB索引类型怎么实现

本文小编为大家详细介绍“MongoDB索引类型怎么实现”,内容详细,步骤清晰,细节处理妥当,希望这篇“MongoDB索引类型怎么实现”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。MongoDB 4.2官方支持索引
2023-06-29

MongoDB中使用hash索引有什么限制

在MongoDB中,使用hash索引有以下限制:Hash索引仅适用于精确匹配查询,不支持范围查询或排序操作。在对大型数据集进行哈希索引构建时,可能会导致内存消耗过多的情况。由于哈希函数的不可逆性,无法对哈希索引进行范围查询或排序操作。
MongoDB中使用hash索引有什么限制
2024-04-19

MongoDB中二级索引和主索引的区别是什么

MongoDB中的主索引是指集合中的_id字段,默认情况下_id字段会被自动创建并作为主索引。主索引是集合中的唯一索引,它保证了集合中每个文档都有一个唯一的标识。主索引在MongoDB中是唯一的,且不能被删除。而二级索引是指除主索引外的其
MongoDB中二级索引和主索引的区别是什么
2024-04-19

编程热搜

目录