C#使用MongoDB-第三章 索引的管理

索引管理接口:IMongoIndexManager<TDocument>

要进行集合索引的管理,需要获得IMongoIndexManager<TDocument>实例对象,这个实例对象可以从集合对象的Indexes属性获取。

  • IMongoIndexManager<TDocument> IndexesIMongoCollection<TDocument>的实例方法,获取集合的索引管理对象。

一、索引的创建

索引的配置模型

CreateIndexModel CreateIndexModel(IndexKeysDefinition<TDocument> keys[, CreateIndexOptions options])CreateIndexModel类的构造函数。

  • keys:设置索引的字段,可以直接使用Json字符串,例如{Name:1, Age: -1}
  • options:索引的一些配置选项,CreateIndexOptions类型,其中有几个常用的属性设置。
    • Name:索引的名称。
    • Unique:是否创建唯一索引,创建时,如果集合中已有数据,那么唯一索引的字段值不能重复,否则报异常。默认为false
    • Background:默认情况下创建索引时会阻塞线程,设置为true时表示后台进行创建,不阻塞线程。
csharp 复制代码
var options = new CreateIndexOptions { Name="Age_Name_Index", Unique=true };
var indexModel = new CreateIndexModel<Student>("{Age:1, Name:-1}", options);

1、创建单个索引

Task<string> CreateOneAsync(CreateIndexModel<TDocument> model)IMongoIndexManager的实例方法,异步创建一个索引,并返回索引的名称。

string CreateOne(CreateIndexModel<TDocument> model)IMongoIndexManager的实例方法,同步创建一个索引,并返回索引的名称。

  • model:创建索引的配置模型,可以用于设置索引的字段和索引的选项。
csharp 复制代码
const string conStr = "mongodb://moo:[email protected]:27017/FirstMongo";
var client = new MongoClient(conStr);
var studentCollection = client.GetDatabase("FirstMongo").GetCollection<Student>("Student");
var indexManager = studentCollection.Indexes;
var options = new CreateIndexOptions { Name="Age_Name_Index", Unique=true };
var indexModel = new CreateIndexModel<Student>("{Age:1, Name:-1}", options);
var indexName = indexManager.CreateOne(indexModel);

2、创建多个索引

IEnumerable<string> CreateManyAsync(IEnumerable<CreateIndexModel<TDocument>> models)IMongoIndexManager的实例方法,异步创建多个索引。

IEnumerable<string> CreateMany(IEnumerable<CreateIndexModel<TDocument>> models)IMongoIndexManager的实例方法,同步创建多个索引。

csharp 复制代码
const string conStr = "mongodb://moo:[email protected]:27017/FirstMongo";
var client = new MongoClient(conStr);
var studentCollection = client.GetDatabase("FirstMongo").GetCollection<Student>("Student");
var indexManager = studentCollection.Indexes;

var indexModel1 = new CreateIndexModel<Student>("{Age:1}", new CreateIndexOptions { Name="Age_Index"});
var indexModel2 = new CreateIndexModel<Student>("{Name:1}", new CreateIndexOptions { Name="Name_Index"});
var indexName = indexManager.CreateMany(new List<CreateIndexModel<Student>>() { indexModel1, indexModel2 });

二、删除索引

DropOne(string indexName)IMongoIndexManager的实例方法,同步删除指定索引。

DropOneAsync(string indexName)IMongoIndexManager的实例方法,异步删除指定索引。

DropAll():同步删除所有索引。

DropAllAsync():异步删除所有索引。

csharp 复制代码
const string conStr = "mongodb://moo:[email protected]:27017/FirstMongo";
var client = new MongoClient(conStr);
var studentCollection = client.GetDatabase("FirstMongo").GetCollection<Student>("Student");
var indexManager = studentCollection.Indexes;
indexManager.DropAll();

三、查询索引

csharp 复制代码
const string conStr = "mongodb://moo:[email protected]:27017/FirstMongo";
var client = new MongoClient(conStr);
var studentCollection = client.GetDatabase("FirstMongo").GetCollection<Student>("Student");
var bsons = studentCollection.Indexes.List().ToList();
foreach (var index in bsons)
{
    Console.WriteLine(index.ToJson());
}
相关推荐
程序员拂雨1 小时前
MongoDB知识框架
数据库·mongodb
半新半旧1 小时前
mongodb 学习笔记
笔记·学习·mongodb
明月看潮生2 小时前
青少年编程与数学 02-019 Rust 编程基础 05课题、复合数据类型
开发语言·青少年编程·rust·编程与数学
幼稚诠释青春2 小时前
Java学习笔记(对象)
java·开发语言
XYR1212122 小时前
C# 参数
c#
Wyc724093 小时前
JDBC:java与数据库连接,Maven,MyBatis
java·开发语言·数据库
强化学习与机器人控制仿真3 小时前
Newton GPU 机器人仿真器入门教程(零)— NVIDIA、DeepMind、Disney 联合推出
开发语言·人工智能·python·stm32·深度学习·机器人·自动驾驶
oMMh3 小时前
使用C# ASP.NET创建一个可以由服务端推送信息至客户端的WEB应用(2)
前端·c#·asp.net
Risehuxyc4 小时前
GrassRoot备份项目
c#
芯片SIPI设计4 小时前
MIPI C-PHY 标准学习----一种通用多信号传输方案
c语言·开发语言·学习