C# 如何通过 .NET 在 MongoDB 中创建索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17807577/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 10:31:18  来源:igfitidea点击:

How to create indexes in MongoDB via .NET

c#.netmongodbindexingmongodb-.net-driver

提问by Payedimaunt

I've programmatically created a new document collection using the MongoDB C# driver.

我已经使用 MongoDB C# 驱动程序以编程方式创建了一个新的文档集合。

At this point I want to create and build indexes programmatically. How can I do that?

在这一点上,我想以编程方式创建和构建索引。我怎样才能做到这一点?

采纳答案by i3arnon

Starting from v2.0 of the driver there's a new async-only API. The old API should no longer be used as it's a blocking facade over the new API and is deprecated.

从驱动程序的 v2.0 开始,有一个新的async-only API。不应再使用旧 API,因为它是新 API 的阻塞外观,已弃用。

The currently recommended way to create an index is by calling and awaiting CreateOneAsyncwith an IndexKeysDefinitionyou get by using Builders.IndexKeys:

目前推荐的方法来创建一个索引是通过调用并等待CreateOneAsyncIndexKeysDefinition您通过使用得到Builders.IndexKeys

static async Task CreateIndex()
{
    var client = new MongoClient();
    var database = client.GetDatabase("HamsterSchool");
    var collection = database.GetCollection<Hamster>("Hamsters");
    await collection.Indexes.CreateOneAsync(Builders<Hamster>.IndexKeys.Ascending(_ => _.Name));
}

回答by Derick

Something like this should do:

这样的事情应该做:

var server = MongoServer.Create("mongodb://localhost");
var db = server.GetDatabase("myapp");

var users = db.GetCollection<User>("users");

users.EnsureIndex(new IndexKeysBuilder().Ascending("EmailAddress"));

Please see the following bits in the documentation:

请参阅文档中的以下位:

回答by i3arnon

you should use CreateIndexas EnsureIndexis marked obsolete for future compatibility with the next versions of MongoDB:

您应该使用CreateIndexasEnsureIndex标记为过时,以便将来与以下版本的下一个版本兼容MongoDB

var client = new MongoClient("mongodb://localhost");
var db = client.GetServer().GetDatabase("db");
var collection = db.GetCollection<Hamster>("Hamsters");

collection.CreateIndex(IndexKeys<Hamster>.Ascending(_ => _.Name));

回答by Craig

There is an entire area on Indexing under the Definitions and Builders documentation page:

在定义和构建器文档页面下有一个完整的索引区域:

http://mongodb.github.io/mongo-csharp-driver/2.4/reference/driver/definitions/#index-keys

http://mongodb.github.io/mongo-csharp-driver/2.4/reference/driver/definitions/#index-keys

Example:

例子:

IndexKeysDefinition<MyModel> keys = "{ Reference: 1 }";
var indexModel = new CreateIndexModel<MyModel>(keys);
await _context.Indexes.CreateOneAsync(indexModel);

回答by Stephen Kennedy

The overload of CreateOneAsyncin the currently accepted answer is now marked as obsolete with the message "Use CreateOneAsync with a CreateIndexModel instead." Here's how you do it:

CreateOneAsync当前接受的答案中的的重载现在被标记为已过时,并带有“使用 CreateOneAsync 和 CreateIndexModel 代替”消息。这是你如何做到的:

static async Task CreateIndex(string connectionString)
{
    var client = new MongoClient(connectionString);
    var database = client.GetDatabase("HamsterSchool");
    var collection = database.GetCollection<Hamster>("Hamsters");
    var indexOptions = new CreateIndexOptions();
    var indexKeys = Builders<Hamster>.IndexKeys.Ascending(hamster => hamster.Name);
    var indexModel = new CreateIndexModel<Hamster>(indexKeys, indexOptions);
    await collection.Indexes.CreateOneAsync(indexModel);
}

回答by ?? ΝιΓΞΗΛψΚ

the easiest way to create indexes in c# is by using the driver wrapper library MongoDB.Entities. here's an example of creating a text index:

在 c# 中创建索引的最简单方法是使用驱动程序包装库MongoDB.Entities。这是创建文本索引的示例:

    DB.Index<Author>()
      .Key(a => a.Name, Type.Text)
      .Key(a => a.Surname, Type.Text)
      .Create();

and to do a full-text search, you simply do:

要进行全文搜索,您只需执行以下操作:

    DB.SearchText<Author>("search term");

haven't seen anything else that makes it simpler than that.

还没有看到任何其他东西使它变得更简单。