索引管理接口:IMongoIndexManager<TDocument>
要進(jìn)行集合索引的管理,需要獲得IMongoIndexManager<TDocument>
實(shí)例對象,這個實(shí)例對象可以從集合對象的Indexes
屬性獲取。
-
IMongoIndexManager<TDocument> Indexes
:IMongoCollection<TDocument>
的實(shí)例方法,獲取集合的索引管理對象。
一、索引的創(chuàng)建
索引的配置模型
CreateIndexModel CreateIndexModel(IndexKeysDefinition<TDocument> keys[, CreateIndexOptions options])
:CreateIndexModel
類的構(gòu)造函數(shù)。
-
keys
:設(shè)置索引的字段,可以直接使用Json字符串,例如{Name:1, Age: -1}
-
options
:索引的一些配置選項(xiàng),CreateIndexOptions
類型,其中有幾個常用的屬性設(shè)置。-
Name
:索引的名稱。 -
Unique
:是否創(chuàng)建唯一索引,創(chuàng)建時,如果集合中已有數(shù)據(jù),那么唯一索引的字段值不能重復(fù),否則報(bào)異常。默認(rèn)為false
-
Background
:默認(rèn)情況下創(chuàng)建索引時會阻塞線程,設(shè)置為true
時表示后臺進(jìn)行創(chuàng)建,不阻塞線程。
-
var options = new CreateIndexOptions { Name="Age_Name_Index", Unique=true };
var indexModel = new CreateIndexModel<Student>("{Age:1, Name:-1}", options);
1、創(chuàng)建單個索引
Task<string> CreateOneAsync(CreateIndexModel<TDocument> model)
:IMongoIndexManager
的實(shí)例方法,異步創(chuàng)建一個索引,并返回索引的名稱。
string CreateOne(CreateIndexModel<TDocument> model)
:IMongoIndexManager
的實(shí)例方法,同步創(chuàng)建一個索引,并返回索引的名稱。
-
model
:創(chuàng)建索引的配置模型,可以用于設(shè)置索引的字段和索引的選項(xiàng)。
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、創(chuàng)建多個索引
IEnumerable<string> CreateManyAsync(IEnumerable<CreateIndexModel<TDocument>> models)
:IMongoIndexManager
的實(shí)例方法,異步創(chuàng)建多個索引。
IEnumerable<string> CreateMany(IEnumerable<CreateIndexModel<TDocument>> models)
:IMongoIndexManager
的實(shí)例方法,同步創(chuàng)建多個索引。
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
的實(shí)例方法,同步刪除指定索引。
DropOneAsync(string indexName)
:IMongoIndexManager
的實(shí)例方法,異步刪除指定索引。
DropAll()
:同步刪除所有索引。
DropAllAsync()
:異步刪除所有索引。文章來源:http://www.zghlxwxcb.cn/news/detail-813573.html
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());
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-813573.html
到了這里,關(guān)于C#使用MongoDB-第三章 索引的管理的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!