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:123456@127.0.0.1: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:123456@127.0.0.1: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:123456@127.0.0.1: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:123456@127.0.0.1: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());
}
相关推荐
Franklin19 分钟前
Python界面设计【QT-creator基础编程 - 01】如何让不同分辨率图像自动匹配graphicsView的窗口大小
开发语言·python·qt
谷宇.22 分钟前
【Unity3D实例-功能-拔枪】角色拔枪(二)分割上身和下身
游戏·unity·c#·游戏程序·unity3d·游戏开发·游戏编程
郝学胜-神的一滴32 分钟前
深入理解QFlags:Qt中的位标志管理工具
开发语言·c++·qt·程序人生
LZQqqqqo43 分钟前
C# 中 ArrayList动态数组、List<T>列表与 Dictionary<T Key, T Value>字典的深度对比
windows·c#·list
柯南二号1 小时前
【Java后端】MyBatis-Plus 原理解析
java·开发语言·mybatis
我是哈哈hh2 小时前
【Node.js】ECMAScript标准 以及 npm安装
开发语言·前端·javascript·node.js
Sammyyyyy3 小时前
2025年,Javascript后端应该用 Bun、Node.js 还是 Deno?
开发语言·javascript·node.js
Dm_dotnet3 小时前
Stylet启动机制详解:从Bootstrap到View显示
c#
William一直在路上4 小时前
Python数据类型转换详解:从基础到实践
开发语言·python
看到我,请让我去学习4 小时前
Qt— 布局综合项目(Splitter,Stacked,Dock)
开发语言·qt