如何通过.NET创建MongoDB的索引 [英] How to create indexes in MongoDB via .NET

查看:607
本文介绍了如何通过.NET创建MongoDB的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MongoDB的C#编程驱动程序创建一个新的文档集合。

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

在这一点上我想创造和建立索引编程。我该怎么办呢?

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

推荐答案

你应该使用的CreateIndex EnsureIndex 是标记为已过时与下一个版本未来的兼容性的MongoDB

you should use CreateIndex as EnsureIndex is marked obsolete for future compatibility with the next versions of 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));

这篇关于如何通过.NET创建MongoDB的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆