






















距离上一篇文字过去已经很久了。一方面是工作有点忙,另外也是遇到点问题,到现在也没有解决。这个问题我会在文章的结尾在提出来,现在先进入这篇文章的主题吧——MongoDB入门。
由于我完全是一个彻彻底底的初学者,所以写的都只是非常非常粗浅的入门内容,各位看官不要鄙视~~~~
///<summary>
/// 连接数据库
///</summary>
///<param name="info"></param>
public static void Connect(DbInfo info)
{
DbHelper.info = info;
if (info.DbType == DbType.MongoDB)
{
MongoServerSettings mss = new MongoServerSettings();
mss.ConnectionMode = ConnectionMode.Direct; // 数据库连接模式
//mss.ConnectionMode = ConnectionMode.ReplicaSet; // 这个是针对数据库集的(多个数据库地址)
mss.ConnectTimeout = new TimeSpan(0, 0, 20); // 超时时间 20s
mss.Server = new MongoServerAddress(info.Source, info.Port); // 数据库
// mss.Servers是针对数据库集的//mss.DefaultCredentials = new MongoCredentials(info.Uid, info.Pwd, true);
MongoServer ms = new MongoServer(mss);
MongoDatabaseSettings mds = new MongoDatabaseSettings(ms, info.DbName);ms.Connect();
db = new MongoDatabase(ms, mds); // 保存MongoDatabase信息,以便后续调用
}
}
BsonClassMap.RegisterClassMap<T>(cm =>
{
cm.AutoMap();
});
BsonClassMap.RegisterClassMap<T>(cm =>
{
cm.AutoMap();
cm.SetIdMember(cm.GetMemberMap(c => c.ID));
cm.IdMemberMap.SetIdGenerator(StringObjectIdGenerator.Instance);
});
[BsonId(IdGenerator = typeof(StringObjectIdGenerator))]
public virtual string ID { get; set; }
[BsonIgnore]
public string FavImage {get; set;}
DbHelper.db.GetCollection<T>(name); // name表示T映射的Collection名称
GetCollection<T>().FindAll();
GetCollection<T>().Find(query).SetSortOrder(sortBy);
GetCollection<T>().Find(query).SetSortOrder(sortBy).SetSkip(skip).SetLimit(limit);
///<summary>
/// 获取标签对应的数据
///</summary>
///<param name="ht"></param>
///<returns></returns>
public static List<HouseInfo> Find(HouseTag ht)
{
QueryComplete query = null;
if (ht != null)
{
query = Query.And(
Query.GTE(HouseTag.PNameArea, ht.AreaMin),
Query.LTE(HouseTag.PNameArea, ht.AreaMax),
Query.GTE(HouseTag.PNamePrice, ht.PriceMin),
Query.LTE(HouseTag.PNamePrice, ht.PriceMax),
Query.GTE(HouseTag.PNamePriceTotal, ht.PriceTotalMin),
Query.LTE(HouseTag.PNamePriceTotal, ht.PriceTotalMax),
Query.GTE(HouseTag.PNameYear, ht.YearMin),
Query.LTE(HouseTag.PNameYear, ht.YearMax)
);
if (!string.IsNullOrEmpty(ht.Zone))
{
MongoDB.Bson.BsonRegularExpression reg = new MongoDB.Bson.BsonRegularExpression(string.Format("*{0}*", ht.Zone));
query = Query.And(query, Query.Matches(HouseTag.PNameZone, reg));
}
}
return MongoHelper.FindAll<HouseInfo>(query, SortBy.Descending("UpdateTime")).ToList();
}
///<summary>
/// 更新数据集合
///</summary>
///<typeparam name="T"></typeparam>
///<param name="listEntity"></param>
public static void Save<T>(IEnumerable<IMongoEntity> listEntity)
where T : class, IMongoEntity
{
if (listEntity != null)
{
MongoCollection<T> col = GetCollection<T>();
foreach (IMongoEntity entity in listEntity)
{
col.Save(entity);
}
}
}
///<summary>
// 删除数据集合
///</summary>
///<typeparam name="T"></typeparam>
///<param name="listEntity"></param>
public static void Remove<T>(IEnumerable<IMongoEntity> listEntity)
where T : class, IMongoEntity
{
if (listEntity != null)
{
MongoCollection<T> col = GetCollection<T>();
foreach (IMongoEntity entity in listEntity)
{
col.Remove(Query.EQ("ID", entity.ID));
}
}
}
在最后说明下我在文章开头的时候提到的之前遇到的问题。
是这样的,我觉得这个房产信息采集器的目标用户主要是些关注房产交易信息的朋友,而这些人总不能都指望他们是IT出身,或者希望他们可以非常顺利的自己安装部署MongoDB吧。所以我就想是否可以在程序运行的时候自己去判断是否有MongoDB的服务,是否已经启动了MongoDB数据库。如果没有服务的话,创建一个服务;如果没有启动的话,就启动相应的服务。总之只要打开房产信息采集器,不需要再做其他的就直接可以用了。可是笔者尝试了n久,没能实现创建MongoDB服务的功能。
我的想法是在发布的时候将MongoDB的相应目录也一起放在程序的根目录。然后在没有相应服务的时候,启动相应的命令行自动创建MongoDB的服务,并启动。
所以这里有两个问题:
这两个问题,当然也可以引申为创建并启动服务(包括其他任何服务)。
不知道各位有没有什么建议或已经实现的方法?如果有的话,评论、私信都欢迎啊。最好能有现成的代码。哈哈。
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。