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时表示后台进行创建,不阻塞线程。
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:创建索引的配置模型,可以用于设置索引的字段和索引的选项。
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的实例方法,同步创建多个索引。

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():异步删除所有索引。

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();

三、查询索引

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());
}

在这里插入图片描述

  • 10
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SchuylerEX

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值