From 23b80f05b47378ee2ad9b09c9443558989298a7d Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Tue, 20 Feb 2024 13:30:34 +0800 Subject: [PATCH] Update document --- docs/en/MongoDB.md | 28 ++++++++++++++++++++++++++++ docs/zh-Hans/MongoDB.md | 28 ++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/docs/en/MongoDB.md b/docs/en/MongoDB.md index 0faa6a7d26..df7f92e779 100644 --- a/docs/en/MongoDB.md +++ b/docs/en/MongoDB.md @@ -82,6 +82,34 @@ If you only need to configure the collection name, you can also use `[MongoColle public IMongoCollection Questions => Collection(); ```` +### Configure Indexes and CreateCollectionOptions for a Collection + +You can configure indexes and `CreateCollectionOptions` for your collections by overriding the `CreateModel` method. Example: + +````csharp +protected override void CreateModel(IMongoModelBuilder modelBuilder) +{ + base.CreateModel(modelBuilder); + + modelBuilder.Entity(b => + { + b.CreateCollectionOptions.Collation = new Collation(locale:"en_US", strength: CollationStrength.Secondary); + b.ConfigureIndexes(indexes => + { + indexes.CreateOne( + new CreateIndexModel( + Builders.IndexKeys.Ascending("MyProperty"), + new CreateIndexOptions { Unique = true } + ) + ); + } + ); + }); +} +```` + +This example sets a collation for the collection and creates a unique index for the `MyProperty` property. + ### Configure the Connection String Selection If you have multiple databases in your application, you can configure the connection string name for your DbContext using the `[ConnectionStringName]` attribute. Example: diff --git a/docs/zh-Hans/MongoDB.md b/docs/zh-Hans/MongoDB.md index 540793dd1f..1602c61a38 100644 --- a/docs/zh-Hans/MongoDB.md +++ b/docs/zh-Hans/MongoDB.md @@ -51,6 +51,34 @@ public class MyDbContext : AbpMongoDbContext * 为每一个mongo集合添加一个公共的 `IMongoCollection` 属性.ABP默认使用这些属性创建默认的仓储 * 重写 `CreateModel` 方法,可以在方法中配置集合(如设置集合在数据库中的名字) +### 为集合配置索引和 CreateCollectionOptions + +你可以在 `CreateModel` 方法中配置集合的索引和 `CreateCollectionOptions`: + +````csharp +protected override void CreateModel(IMongoModelBuilder modelBuilder) +{ + base.CreateModel(modelBuilder); + + modelBuilder.Entity(b => + { + b.CreateCollectionOptions.Collation = new Collation(locale:"en_US", strength: CollationStrength.Secondary); + b.ConfigureIndexes(indexes => + { + indexes.CreateOne( + new CreateIndexModel( + Builders.IndexKeys.Ascending("MyProperty"), + new CreateIndexOptions { Unique = true } + ) + ); + } + ); + }); +} +```` + +在这个例子中,我们设置了集合的排序规则和一个唯一的索引. + ### 将 Db Context 注入到依赖注入中 在你的模块中使用 `AddAbpDbContext` 方法将Db Context注入到[依赖注入](Dependency-Injection.md)系统中.