在 app.seeting 文件中添加多个 MongoDb 集合以及如何从服务 class 中调用和使用它们
Adding Multiple MongoDb Collections in app.seeting file and how can they be called and used from service class
我有多个集合,例如一个用于用户、课程、行动计划和疾病的集合,我如何将它们添加到 appsettings.json 以及如何在使用它们时在服务 class 中单独调用它们
appsetting.json 文件
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"DatabaseSettings": {
"ConnectionString": "mongodb://localhost:27017",
"DatabaseName": "local",
"CollectionName": "startup_log"
}
}
启动文件
services.AddScoped<DataInterface, DataService>();
services.Configure<AppDbConfig>(Configuration.GetSection("DatabaseSettings"));
AppDbConfig Class
public class AppDbConfig
{
public string ConnectionString { get; set; } = null!;
public string DatabaseName { get; set; } = null!;
public string CollectionName { get; set; } = null!;
}
数据服务Class
private readonly IMongoCollection<startup_log> startupLogs;
public DataService(IOptions<AppDbConfig> settings)
{
MongoClient client = new MongoClient(settings.Value.ConnectionString);
IMongoDatabase database = client.GetDatabase(settings.Value.DatabaseName);
startupLogs = database.GetCollection< startup_log >(settings.Value.CollectionName);
}
启动日志文件
public class startup_log
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonElement("hostname")]
public string HostName { get; set; } = null!;
[BsonElement("startTime")]
public DateTime StartTime { get; set; }
[BsonElement("startTimeLocal")]
public string StartTimeLocal { get; set; } = null!;
}
为了支持不同的集合,您可以在您的配置中重命名 CollectionName
属性 并为您需要支持的每个集合创建一个 属性,例如
public class AppDbConfig
{
public string ConnectionString { get; set; } = null!;
public string DatabaseName { get; set; } = null!;
public string StartupLogsCollectionName { get; set; } = null!;
public string OtherCollectionName { get; set; } = null!;
}
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"DatabaseSettings": {
"ConnectionString": "mongodb://localhost:27017",
"DatabaseName": "local",
"StartupLogsCollectionName": "startup_log",
"OtherCollectionName": "otherDocs"
}
}
在您的 DataService
中,您可以在设置服务时使用不同的集合名称:
private readonly IMongoCollection<startup_log> startupLogs;
private readonly IMongoCollection<OtherType> otherColl;
public DataService(IOptions<AppDbConfig> settings)
{
MongoClient client = new MongoClient(settings.Value.ConnectionString);
IMongoDatabase database = client.GetDatabase(settings.Value.DatabaseName);
startupLogs = database.GetCollection<startup_log>(settings.Value.StartupLogsCollectionName);
otherColl = database.GetCollection<OtherType>(settings.Value.OtherCollectionName);
}
我有多个集合,例如一个用于用户、课程、行动计划和疾病的集合,我如何将它们添加到 appsettings.json 以及如何在使用它们时在服务 class 中单独调用它们
appsetting.json 文件
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"DatabaseSettings": {
"ConnectionString": "mongodb://localhost:27017",
"DatabaseName": "local",
"CollectionName": "startup_log"
}
}
启动文件
services.AddScoped<DataInterface, DataService>();
services.Configure<AppDbConfig>(Configuration.GetSection("DatabaseSettings"));
AppDbConfig Class
public class AppDbConfig
{
public string ConnectionString { get; set; } = null!;
public string DatabaseName { get; set; } = null!;
public string CollectionName { get; set; } = null!;
}
数据服务Class
private readonly IMongoCollection<startup_log> startupLogs;
public DataService(IOptions<AppDbConfig> settings)
{
MongoClient client = new MongoClient(settings.Value.ConnectionString);
IMongoDatabase database = client.GetDatabase(settings.Value.DatabaseName);
startupLogs = database.GetCollection< startup_log >(settings.Value.CollectionName);
}
启动日志文件
public class startup_log
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; set; }
[BsonElement("hostname")]
public string HostName { get; set; } = null!;
[BsonElement("startTime")]
public DateTime StartTime { get; set; }
[BsonElement("startTimeLocal")]
public string StartTimeLocal { get; set; } = null!;
}
为了支持不同的集合,您可以在您的配置中重命名 CollectionName
属性 并为您需要支持的每个集合创建一个 属性,例如
public class AppDbConfig
{
public string ConnectionString { get; set; } = null!;
public string DatabaseName { get; set; } = null!;
public string StartupLogsCollectionName { get; set; } = null!;
public string OtherCollectionName { get; set; } = null!;
}
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*",
"DatabaseSettings": {
"ConnectionString": "mongodb://localhost:27017",
"DatabaseName": "local",
"StartupLogsCollectionName": "startup_log",
"OtherCollectionName": "otherDocs"
}
}
在您的 DataService
中,您可以在设置服务时使用不同的集合名称:
private readonly IMongoCollection<startup_log> startupLogs;
private readonly IMongoCollection<OtherType> otherColl;
public DataService(IOptions<AppDbConfig> settings)
{
MongoClient client = new MongoClient(settings.Value.ConnectionString);
IMongoDatabase database = client.GetDatabase(settings.Value.DatabaseName);
startupLogs = database.GetCollection<startup_log>(settings.Value.StartupLogsCollectionName);
otherColl = database.GetCollection<OtherType>(settings.Value.OtherCollectionName);
}