时间:2021-07-01 10:21:17 帮助过:6人阅读
public class UserBLL
{
public string connectionString = "mongodb://localhost";
public string databaseName = "myDatabase";
private Mongo mongo;
private MongoDatabase mongoDatabase;
//注意这里泛型类型为“UserInfo”
private MongoCollection<UserInfo> mongoCollection;
public UserBLL()
{
mongo = GetMongo();
mongoDatabase = mongo.GetDatabase(databaseName) as MongoDatabase;
mongoCollection = mongoDatabase.GetCollection<UserInfo>() as MongoCollection<UserInfo>;
mongo.Connect();
}
~UserBLL()
{
mongo.Disconnect();
}
/// <summary>
/// 配置Mongo,将类UserInfo映射到集合
/// </summary>
private Mongo GetMongo()
{
var config = new MongoConfigurationBuilder();
config.Mapping(mapping =>
{
mapping.DefaultProfile(profile =>
{
profile.SubClassesAre(t => t.IsSubclassOf(typeof(UserInfo)));
});
mapping.Map<UserInfo>();
});
config.ConnectionString(connectionString);
return new Mongo(config.BuildConfiguration());
}
}
三、用MongoDB的文档结构描述数据关系
标签:类型 没有 ase 方式 sar json getc border profile