.Net连接MongoDb:在MongoDb .NET驱动程序中使用索引
阿里云服务器,每月低至7.8元,项目演示即建站必备,比腾讯云更便宜,并且不需学生认证,从此链接购买有效:去购买
介绍在上一篇文章中,我们介绍了MongoDb中提供的几种诊断工具。 我们提到了自动记录所有需要超过100毫秒的查询完成的默认日志记录工具。 然后我们查看了3个工具,它们提供了有关MongoDb中发生的更多细节:profiler,mongotop和mongostat。 他们是伟大的工具,特别是当我们试图优化我们的查询。 他们还通过查看在数据库中执行的实际查询来帮助调试我们的应用程序。 这是MongoDb专用于索引和性能的最后一个职位。 我们将介绍如何使用MongoDb .NET驱动程序中的索引。 MongoDb驱动程序中的索引通常我们直接在mongo shell中处理索引。 但是,如果您喜欢利用C#和Visual Studio,则.NET驱动程序提供了多个索引入口点。 索引设施的网关是一个可以返回索引管理器的集合的Indexes属性。 我们首先列出可用于餐馆馆藏的索引: ModelContext modelContext = ModelContext.Create(new ConfigFileConfigurationRepository(), new AppConfigConnectionStringRepository()); var restaurantsIndexManager = modelContext.Restaurants.Indexes; var restaurantsIndexList = restaurantsIndexManager.List(); while (restaurantsIndexList.MoveNext()) { var currentIndex = restaurantsIndexList.Current; foreach (var doc in currentIndex) { var docNames = doc.Names; foreach (string name in docNames) { var value = doc.GetValue(name); Debug.WriteLine(string.Concat(name, ": ", value)); } } } 上述代码将在调试窗口中打印以下内容: v:1 key:{“_id”:1} name:_id_ ns:model.restaurants v:1 key:{“borough”:1.0,“cuisine”:1.0} name:borough_1_cuisine_1 ns:model.restaurants v:1 key :{“grade.grade”:1.0,“grades.score”:1.0}名称:grade.grade_1_grades.score_1 ns:model.restaurants ...对应于由mongo shell中的getIndexes()命令生成的输出: [ { "v" : 1, "key" : { "_id" : 1 }, "name" : "_id_", "ns" : "model.restaurants" }, { "v" : 1, "key" : { "borough" : 1, "cuisine" : 1 }, "name" : "borough_1_cuisine_1", "ns" : "model.restaurants" }, { "v" : 1, "key" : { "grades.grade" : 1, "grades.score" : 1 }, "name" : "grades.grade_1_grades.score_1", "ns" : "model.restaurants" } ] 索引创建索引管理器提供了4种创建索引的方法:
在讨论CRUD操作之前,我们看到了很多类似的例子。 有一个功能来创建一个或多个索引,并且它们具有异步对等体以及等待的操作。 我们来看一下CreateOne函数的例子。 你还记得过滤器和排序定义等定义吗? 可以使用索引键定义构建器构建索引键定义。 静态Builders类的IndexKeys属性提供了创建索引键定义的入口点。 当你开始输入... Builders<RestaurantDb>.IndexKeys. ...在Visual Studio中,您将看到一些熟悉的方法,如升序,降序和文本。 还有一些更专业的索引类型,我们在本系列中没有看到,它们与地理空间数据类型有关。 Combine方法将两个或多个索引组合成一个复合索引。 Create方法也接受和CreateIndexOptions对象,您可以在其中指定我们之前看到的选项,如索引名称,是否为稀疏等。以下是一个简单的示例,可在餐馆集合的restaurant_id字段中创建索引: ModelContext modelContext = ModelContext.Create(new ConfigFileConfigurationRepository(), new AppConfigConnectionStringRepository()); var restaurantsIndexManager = modelContext.Restaurants.Indexes; var restaurantIndexDefinition = Builders<RestaurantDb>.IndexKeys.Ascending(r => r.Id); string result = restaurantsIndexManager.CreateOne(restaurantIndexDefinition, new CreateIndexOptions() { Name = "RestaurantIdIndexAsc", Background = true }); CreateOne函数返回索引的名称,在这种情况下为“RestaurantIdIndexAsc”。 这是我们在mongo shell中的索引: { "v" : 1, "key" : { "restaurant_id" : 1 }, "name" : "RestaurantIndexAsc", "ns" : "model.restaurants", "background" : true } 删除索引我们可以轻松地删除一个索引名称: ModelContext modelContext = ModelContext.Create(new ConfigFileConfigurationRepository(), new AppConfigConnectionStringRepository()); var restaurantsIndexManager = modelContext.Restaurants.Indexes; restaurantsIndexManager.DropOne("RestaurantIdIndexAsc"); 如果没有找到索引的名称,那么我们得到一个例外: 命令dropIndexes failed:index not found with name [nosuchindex]。 DropOne有一个异步对方。 DropAll和DropAllAsync方法从集合中删除所有索引。 阿里云服务器,每月低至7.8元,项目演示即建站必备,比腾讯云更便宜,并且不需学生认证,从此链接购买有效: 去购买
版权声明:本站所有教程均为本站原创或翻译,转载请注明出处,请尊重他人劳动果实。请记住本站地址:www.yuanjiaocheng.net (猿教程) 作者:卿文刚
本文标题: C#环境 本文地址:http://www.yuanjiaocheng.net/CsharpMongo/39.html |