在 MongoDB 中显示索引
今天,我们将学习如何显示来自一个集合、一个数据库的所有集合以及所有数据库的所有集合的索引。我们还将看到如何从所有数据库的所有集合中获取特定类型的索引。
根据项目需求,我们可以使用以下方式之一在 MongoDB 中显示索引。在本教程中,我们将从特定解决方案到通用解决方案。
在 MongoDB 中使用 getIndexes()
显示来自特定集合的索引
示例代码:
// MongoDB Version 5.0
> db.Client.getIndexes();
输出:
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
},
{
"v" : 2,
"key" : {
"shopPosId" : 1
},
"name" : "shopPosId_1"
}
]
在上面的输出中,我们可以看到两个索引,其中 shopPosId
字段的索引是用户创建的,而 _id
是默认索引。如果你没有为特定集合创建任何索引,则只会将 _id
视为索引。
为了获得即时的 mongo shell 帮助,我们还可以使用下面给出的任何命令。
// MongoDB Version 5.0
> help;
> db.help();
> db.test.help();
我们还可以使用 stats().indexSizes
来显示特定集合中具有相应大小的索引。
示例代码:
// MongoDB Version 5.0
> db.Client.stats().indexSizes
输出:
{ "_id_" : 36864, "shopPosId_1" : 20480 }
使用 forEach()
显示 MongoDB 数据库中所有集合的索引
示例代码:
// MongoDB Version 5.0
> db.getCollectionNames().forEach(function(collection) {
all_indexes = db.getCollection(collection).getIndexes();
print("All Indexes for the " + collection + " collection:");
printjson(all_indexes);
});
输出:
All Indexes for the Client collection:
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
},
{
"v" : 2,
"key" : {
"shopPosId" : 1
},
"name" : "shopPosId_1"
}
]
All Indexes for the Shop collection:
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" } ]
All Indexes for the collection1 collection:
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" } ]
All Indexes for the collection2 collection:
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" } ]
All Indexes for the employee collection:
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
},
{
"v" : 2,
"key" : {
"_fts" : "text",
"_ftsx" : 1
},
"name" : "emp_code_text",
"weights" : {
"emp_code" : 1
},
"default_language" : "english",
"language_override" : "language",
"textIndexVersion" : 3
}
]
All Indexes for the printjson collection:
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" } ]
All Indexes for the student_courses collection:
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" } ]
此示例代码显示来自当前数据库中所有集合的所有索引。在此代码片段中,getCollectionNames()
方法从所选数据库中检索所有集合名称,这些名称使用 forEach()
一次一个地传递给匿名函数。
在 forEach()
中,我们获取指定集合的所有索引并将它们保存到 all_indexes
对象中。最后,我们使用 printjson()
在控制台上打印 all_indexes
对象(在我们的例子中是 mongo shell)。
显示来自 MongoDB 中所有数据库的所有集合的索引
示例代码:
// MongoDB Version 5.0
> db.adminCommand("listDatabases").databases.forEach(function(database){
let mdb = db.getSiblingDB(database.name);
mdb.getCollectionInfos({ type: "collection" }).forEach(function(collection){
let currentCollection = mdb.getCollection(collection.name);
let all_indexes = currentCollection.getIndexes();
print("All indexes on the " + database.name + "." + collection.name + ":");
printjson(all_indexes)
});
});
输出:
All indexes on the admin.system.version:
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" } ]
All indexes on the config.system.sessions:
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
},
{
"v" : 2,
"key" : {
"lastUse" : 1
},
"name" : "lsidTTLIndex",
"expireAfterSeconds" : 1800
}
]
All indexes on the local.startup_log:
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" } ]
All indexes on the test.Client:
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
},
{
"v" : 2,
"key" : {
"shopPosId" : 1
},
"name" : "shopPosId_1"
}
]
All indexes on the test.Shop:
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" } ]
All indexes on the test.collection1:
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" } ]
All indexes on the test.collection2:
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" } ]
All indexes on the test.employee:
[
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
},
{
"v" : 2,
"key" : {
"_fts" : "text",
"_ftsx" : 1
},
"name" : "emp_code_text",
"weights" : {
"emp_code" : 1
},
"default_language" : "english",
"language_override" : "language",
"textIndexVersion" : 3
}
]
All indexes on the test.printjson:
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" } ]
All indexes on the test.student_courses:
[ { "v" : 2, "key" : { "_id" : 1 }, "name" : "_id_" } ]
上面给出的示例代码将遍历所有数据库。在每个数据库中,它遍历所有集合。
在每个集合中,它获取所有索引并使用它们各自的数据库和集合名称打印它们。
以下是我们用于上述代码片段的所有方法的简要说明。
有没有办法查看特定集合的隐藏索引?是的。我们可以使用 listIndexes
查看它们,如下所示。
这里,Client
是集合名称。你可以将其替换为你的集合名称。
示例代码:
// MongoDB Version 5.0
> db.runCommand (
{
listIndexes: "Client"
}
);
输出:
{
"cursor" : {
"id" : NumberLong(0),
"ns" : "test.Client",
"firstBatch" : [
{
"v" : 2,
"key" : {
"_id" : 1
},
"name" : "_id_"
},
{
"v" : 2,
"key" : {
"shopPosId" : 1
},
"name" : "shopPosId_1"
}
]
},
"ok" : 1
}
上面的输出包含 firstBatch
,它描述了索引信息,包括用于创建索引的键和选项。从开始 MongoDB 4.4 开始,索引选项 hidden 仅在其值为 true
时才存在。
仅显示 MongoDB 中所有数据库的所有集合中的 text
索引
示例代码:
// MongoDB Version 5.0
> db.adminCommand("listDatabases").databases.forEach(function(database){
let mdb = db.getSiblingDB(database.name);
mdb.getCollectionInfos({ type: "collection" }).forEach(function(collection){
let currentCollection = mdb.getCollection(collection.name);
currentCollection.getIndexes().forEach(function(index){
let indexValues = Object.values(Object.assign({}, index.key));
if (indexValues.includes("text")) {
print("Text index: " + index.name + " on the " +
database.name + "." + collection.name);
printjson(index);
};
});
});
});
输出:
Text index: emp_code_text on the test.employee
{
"v" : 2,
"key" : {
"_fts" : "text",
"_ftsx" : 1
},
"name" : "emp_code_text",
"weights" : {
"emp_code" : 1
},
"default_language" : "english",
"language_override" : "language",
"textIndexVersion" : 3
}
此代码示例与前一个示例类似,不同之处在于我们仅针对所有数据库的所有集合中的 text
类型索引。
相关文章
在 MongoDB Shell 中列出所有数据库
发布时间:2023/05/11 浏览次数:180 分类:MongoDB
-
交互式 Mongo Shell 提供了多个用于获取数据的选项。 本文介绍了在 Mongo Shell 中列出数据库的几种不同方法。
MongoDB 中检查字段包含的字符串
发布时间:2023/05/11 浏览次数:1024 分类:MongoDB
-
这篇文章解决了如何在 MongoDB 中使用正则表达式来确定字段是否包含字符串。在 MongoDB 中使用正则表达式 正则表达式 (regex) 是定义搜索模式的文本字符串。
在 MongoDB 中 upsert 更新插入
发布时间:2023/05/11 浏览次数:214 分类:MongoDB
-
在 MongoDB 中,upsert 结合了更新和插入命令。 它可以在 update() 和 findAndModify() 操作中使用。MongoDB 中的 upsert 查询 upsert 采用单个布尔参数。
如何卸载 MongoDB
发布时间:2023/05/11 浏览次数:745 分类:MongoDB
-
要从您的计算机中卸载 MongoDB,您必须先删除 MongoDB 服务、数据库和日志文件。使用这篇 MongoDB 文章,您将能够从 Ubuntu Linux、Mac 和 Windows 卸载 MongoDB。 请务必保留数据备份,因为一旦卸载,便
在 MongoDB 中存储日期和时间
发布时间:2023/05/11 浏览次数:762 分类:MongoDB
-
本 MongoDB 教程解释了 Date() 对象是什么以及如何使用 Date() 方法对集合进行排序。 这也将帮助您找到在 MongoDB 中显示和存储日期/时间的最佳方法。
MongoDB 按 ID 查找
发布时间:2023/05/11 浏览次数:1856 分类:MongoDB
-
MongoDB 中的 find by Id() 函数用于获取与用户提供的 id 相匹配的文档。 如果找不到与指定 ID 匹配的文档,则返回空值。
检查 MongoDB 服务器是否正在运行
发布时间:2023/05/11 浏览次数:247 分类:MongoDB
-
这篇 MongoDB 教程将告诉您如何检查是否安装了 MongoDB 以及安装的 MongoDB 服务器的版本。 它在 Windows、UBUNTU 和 MAC 等不同的操作系统中实现。
MongoDB 中的分页
发布时间:2023/05/11 浏览次数:174 分类:MongoDB
-
这篇文章将介绍什么是 MongoDB 中的分页。 为什么在 MongoDB 中需要分页以及在 MongoDB 中完成分页的不同方法或方式是什么。
MongoDB 从查询开始
发布时间:2023/05/11 浏览次数:186 分类:MongoDB
-
在这篇 MongoDB 文章中,用户将学习如何使用 $regex 进行开始查询。 它为查询中的模式匹配字符串提供正则表达式功能。