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

在这里插入图片描述

相关推荐

  1. Mongodb索引使用限制

    2024-01-12 19:24:04       30 阅读
  2. MongoDB】--MongoDB组合索引

    2024-01-12 19:24:04       32 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-12 19:24:04       98 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-12 19:24:04       106 阅读
  3. 在Django里面运行非项目文件

    2024-01-12 19:24:04       87 阅读
  4. Python语言-面向对象

    2024-01-12 19:24:04       96 阅读

热门阅读

  1. 五个大幅提升开发效率的VS Code技巧

    2024-01-12 19:24:04       51 阅读
  2. C语言——字符串常量初始化

    2024-01-12 19:24:04       54 阅读
  3. Redis的过期策略

    2024-01-12 19:24:04       54 阅读
  4. 【PostgreSQL】表管理-分区表

    2024-01-12 19:24:04       43 阅读
  5. QT 设置鼠标样式

    2024-01-12 19:24:04       52 阅读
  6. 【mysql】Mac安装mysql

    2024-01-12 19:24:04       55 阅读
  7. uniapp生命周期

    2024-01-12 19:24:04       53 阅读
  8. TDengine 签约积成电子

    2024-01-12 19:24:04       57 阅读