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

发布时间:2024-01-12 19:12:37

索引管理接口: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());
}

在这里插入图片描述

文章来源:https://blog.csdn.net/jjailsa/article/details/135559083
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。