创建索引
索引支持MongoDB中查询的高效执行。如果您的应用程序反复在相同的字段上运行查询,您可以在这些字段上创建索引以提高这些查询的性能。
要创建索引,请使用createIndex()
命令或您的驱动程序的等效方法。此页面展示了MongoDB Shell和驱动程序的示例。
关于此任务
当您在MongoDB Shell或驱动程序中运行创建索引命令时,如果不存在具有相同指定的索引,MongoDB才会创建索引。
尽管索引可以提高查询性能,但添加索引会对写操作的性能产生负面影响。对于写入与读取比例高的集合,索引成本较高,因为每次插入和更新都必须更新任何索引。
步骤
➤要设置此页面上示例的语言,请使用右侧导航栏中的 选择您的语言 下拉菜单。
在mongosh
中创建索引,请使用 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> );
使用异步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>)
要使用Kotlin协程驱动程序创建索引,请使用MongoCollection.createIndex()方法。
collection.createIndex(<key and index type specification>, <options>)
要使用电机驱动器,请使用motor.motor_asyncio.AsyncIOMotorCollection.create_index
。
await db.collection.create_index([(<key and index type specification>)], <options> )
要使用Node.js 驱动器创建索引,请使用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> ] );
使用PHP驱动程序创建索引,使用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>.Descending("name") );
此示例在 name
字段上创建单个键降序索引
collection.createIndex(Indexes.descending("name"), someCallbackFunction());
此示例在 name
字段上创建单个键降序索引
collection.createIndex(Indexes.descending("name"));
此示例在 name
字段上创建单个键降序索引
collection.createIndex(Indexes.descending("name"))
此示例在 name
字段上创建单个键降序索引
await collection.create_index([("name", pymongo.DESCENDING)])
此示例在 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.DESCENDING)])
此示例在 name
字段上创建单个键降序索引
client[:collection].indexes.create_one({ name: -1 })
此示例在 name
字段上创建单个键降序索引
collection.createIndex(descending("name"))
注意
索引排序顺序
使用降序单字段索引可能会对索引性能产生负面影响。为了获得最佳性能,请仅使用升序单字段索引。
结果
要确认已创建索引,请使用 mongosh
运行 db.collection.getIndexes()
方法
db.collection.getIndexes()
输出
[ { v: 2, key: { _id: 1 }, name: '_id_' }, { v: 2, key: { name: -1 }, name: 'name_-1' } ]
要使用驱动程序查看创建的索引信息,请参阅您的 驱动程序文档。