using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MongoDB.Bson;
using MongoDB.Driver.Builders;
using MongoDB.Driver;
using System.Configuration;
/// <summary>
/// MongoDbHelper 的摘要说明
/// </summary>
public class MongoDbHelper
{
public static readonly string mongodbStr = ConfigurationManager.ConnectionStrings["mongodbStr"].ConnectionString;
public MongoDbHelper()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region MyRegion 获取collection对象
public static MongoCollection GetMongoCollection(string dbName, string collectionName)
{
MongoClient client = new MongoClient(mongodbStr);
MongoServer servcer = client.GetServer();
MongoCollection collection = servcer.GetDatabase(MyenumHelper.DBChose.myfirstmog.ToString()).GetCollection(MyenumHelper.TableChose.person.ToString());
//MongoServer server = MongoServer.Create(mongodbStr); //这个方法为否决的方法,还是使用上面的链接方法
//MongoDatabase db = server.GetDatabase(dbName);
//MongoCollection collection = db.GetCollection(collectionName);
return collection;
}
#endregion
#region MyRegion 获取collection对象 泛型
public static MongoCollection GetMongoCollection<T>(string dbName, string collectionName) where T : BaseMongo
{
MongoServer server = MongoServer.Create(mongodbStr);
MongoDatabase db = server.GetDatabase(dbName);
MongoCollection collection = db.GetCollection<T>(collectionName);
return collection;
}
#endregion
#region MyRegion 新增的操作 ok
public static bool Insert<T>(string dbName, string collectionName, T model) where T : BaseMongo
{
try
{
if (model == null)
{
throw new ArgumentNullException("model", "待插入数据不能为空");
}
var collection = GetMongoCollection(MyenumHelper.DBChose.myfirstmog.ToString(), MyenumHelper.TableChose.person.ToString());
collection.Insert(model);//直接插入对象就是ok的
//WriteConcernResult result= collection.Insert<T>(model);//不可以这样使用,下面两句有bug 调试时发现返回为null,不理解,实际新增到数据库是成功的?why?why?why?
// return result.HasLastErrorMessage; result.Ok;
return true;
}
catch (Exception)
{
return false;
}
}
#endregion
#region MyRegion 删除collection 中所有的数据,不可以回滚慎重!!! ok
public static bool DeleteAll(string dbName, string collectionName)
{
try
{
MongoCollection collection = GetMongoCollection(dbName, collectionName);
collection.RemoveAll();
return true;
}
catch (Exception)
{
return false;
}
}
#endregion
#region MyRegion 查找所有的列表信息 ok
public static List<T> FindAll<T>(string dbName, string collectionName) where T : BaseMongo
{
var collection = GetMongoCollection<T>(dbName, collectionName);
return collection.FindAllAs<T>().ToList();
}
#endregion
#region 根据Query的条件来获取一个对象 ok
public static T FindOneByCondition<T>(string dbName, string collectionName, IMongoQuery query) where T : BaseMongo
{
MongoCollection collection = GetMongoCollection(dbName, collectionName);
return collection.FindOneAs<T>(query);
}
#endregion
#region 根据Query的条件来获取一个对象 ok
public static T FindOneById<T>(string dbName, string collectionName, string objid) where T : BaseMongo
{
MongoCollection collection = GetMongoCollection(dbName, collectionName);
return collection.FindOneByIdAs<T>(new ObjectId(objid));
}
#endregion
}