如果您的应用程序对相同字段重复运行查询,请对这些字段创建索引以提高性能。
如要创建索引,请使用 createIndex() Shell 方法或适用于您的驱动程序的等效方法。本页显示 MongoDB Shell 和驱动程序的示例。
关于此任务
在 MongoDB Shell 或驱动程序中运行创建索引的命令时,MongoDB 仅在没有相同规格索引存在时才创建索引。
尽管索引可提高查询性能,但添加索引会对写入操作的性能产生负面影响。对于具有高写入读取比率的集合,索引的成本很高,因为每次插入和更新还必须更新所有索引。
警告
不要在加密字段上创建索引。在使用Queryable Encryption的加密字段上创建索引会对性能产生负面影响。相反,您可以在 __safeContent__字段上创建索引,以支持对加密字段的查询。
步骤
➤ 如要设置此页面上示例的语言,请使用右侧导航窗格中的选择您的语言下拉菜单。
To create an index in mongosh, use db.collection.createIndex().
db.collection.createIndex( <key and index type specification>, <options> )
要使用 .NET 驱动程序创建索引,请使用 MongoCollection.CreateIndex。
collection.CreateIndex( IndexKeys<collection>.<key and index type specification>, <options> );
要使用 Async Java 驱动程序 创建索引,请使用 com.mongodb.async.client.MongoCollection.createIndex 。
collection.createIndex( <key and index type specification>, <options>, <callbackFunction>)
要使用 Java 驱动程序创建索引,请使用 com.mongodb.client.MongoCollection.createIndex。
collection.createIndex( <key and index type specification>, <options> )å
To create an index using the Motor driver, use motor.motor_asyncio.AsyncIOMotorCollection.create_index.
await db.collection.create_index([(<key and index type specification>)], <options> )
To create an index using the Node.JS driver, use createIndex().
collection.createIndex( { <key and index type specification> }, function(err, result) { console.log(result); callback(result); } )
要使用 Perl 驱动程序创建索引,请使用 create_one()。
my $indexes = $db->get_collection( <collection> )->indexes; $indexes->create_one( [ <key and index type specification> ] );
To create an index using the PHP driver, use MongoDB\\Collection::createIndex().
$collection->createIndex(<key and index type specification>, <options>);
要使用 Python 驱动程序创建索引,请使用 pymongo.collection.Collection.create_index方法:
db.collection.create_index([(<key and index type specification>)], <options> )
要使用 Ruby 驱动程序创建索引,请使用 Mongo:: Index:: View #create_one。
client[:collection].indexes.create_one({ <key and index type specification> }, {options})
要使用 Scala 驱动程序创建索引,请使用 org.mongodb.scala.model.Indexes。
collection.createIndex(<key and index type specification>)
例子
此示例会对 name 字段创建一个单键升序索引:
db.collection.createIndex( { name: 1 } )
此示例会对 name 字段创建一个单键升序索引:
collection.CreateIndex( IndexKeys<collection>.Ascending("name") );
此示例会对 name 字段创建一个单键升序索引:
collection.createIndex(Indexes.ascending("name"), someCallbackFunction());
此示例会对 name 字段创建一个单键升序索引:
collection.createIndex(Indexes.ascending("name"));
此示例会对 name 字段创建一个单键升序索引:
await collection.create_index([("name", pymongo.ASCENDING)])
此示例会对 name 字段创建一个单键升序索引:
collection.createIndex( { name : 1 }, function(err, result) { console.log(result); callback(result); } )
此示例会对 name 字段创建一个单键升序索引:
my $indexes = $db->get_collection( <collection> )->indexes; $indexes->create_one( [ name => 1 ] );
此示例会对 name 字段创建一个单键升序索引:
$collection->createIndex(['name' => 1]);
此示例会对 name 字段创建一个单键升序索引:
collection.create_index([("name", pymongo.ASCENDING)])
此示例会对 name 字段创建一个单键升序索引:
client[:collection].indexes.create_one({ name: 1 })
此示例会对 name 字段创建一个单键升序索引:
collection.createIndex(ascending("name"))
结果
Use mongosh to monitor index creation.
要查看集合中存在哪些索引(包括当前正在构建的索引),请运行 db.collection.getIndexes() 方法:
db.collection.getIndexes()
要检查索引是否正在构建,请在 admin数据库上使用 $currentOp 和 db.aggregate()。
以下聚合管道使用$match阶段来显示在 name字段上构建降序索引的活动操作:
db.getSiblingDB("admin").aggregate( [ { $currentOp : { idleConnections: true } }, { $match : {"command.createIndexes": { $exists: true } } } ] )
MongoDB 通过将 active 字段设置为 false,将不同阶段的索引构建(包括等待提交法定人数)标记为空闲连接。idleConnections: true 设置在 $currentOp输出中包含这些空闲连接。
要查看有关使用驱动程序来创建的现有索引的信息,请参阅驱动程序的文档。