志在指尖
用双手敲打未来

C#/.Net操作MongoDBHelper类

先NuGet两个程序集1:MongoDB.Driver、2:MongoDB.Bson
namespaceConsoleApp1
{
///
///MongoDb协助类
///
///
///MongoDb协助类
///
publicclassDB
{
privatestaticreadonlystringconnStr=”mongodb://127.0.0.1:27017″;//GlobalConfig.Settings[“mongoConnStr”];
privatestaticreadonlystringdbName=”school”;//GlobalConfig.Settings[“mongoDbName”];
privatestaticIMongoDatabasedb=null;
privatestaticreadonlyobjectlockHelper=newobject();
privateDB(){}
publicstaticIMongoDatabaseGetDb()
{
if(db==null)
{
lock(lockHelper)
{
if(db==null)
{
varclient=newMongoClient(connStr);
db=client.GetDatabase(dbName);
}
}
}
returndb;
}
}
publicclassMongoDbHelper
{
privateIMongoDatabasedb=null;
privateIMongoCollectioncollection=null;
publicMongoDbHelper()
{
this.db=DB.GetDb();
collection=db.GetCollection(typeof(T).Name);
}
///
///新增
///
///
///
publicTInsert(Tentity)
{
varflag=ObjectId.GenerateNewId();
entity.GetType().GetProperty(“_id”).SetValue(entity,flag);
collection.InsertOneAsync(entity);
returnentity;
}
///
///修正一个值
///
///
///
///
publicboolModify(Expression<Func<T,bool>>express,objectupdateField)
{
if(updateField==null)returnfalse;
varprops=updateField.GetType().GetProperties();
varfield=props[0].Name;
varvalue=props[0].GetValue(updateField);
varupdated=Builders.Update.Set(field,value);
UpdateResultresult=collection.UpdateOneAsync(express,updated).Result;
returnresult.ModifiedCount>0?true:false;
}
///
///更新
///
///
publicboolUpdate(Expression<Func<T,bool>>express,Tentity)
{
try
{
varold=collection.Find(express).ToList().FirstOrDefault();
foreach(varpropinentity.GetType().GetProperties())
{
if(!prop.Name.Equals(“_id”))
{
varnewValue=prop.GetValue(entity);
varoldValue=old.GetType().GetProperty(prop.Name).GetValue(old);
if(newValue!=null)
{
if(oldValue==null)
oldValue=””;
if(!newValue.ToString().Equals(oldValue.ToString()))
{
old.GetType().GetProperty(prop.Name).SetValue(old,newValue.ToString());
}
}
}
}
//varfilter=Builders.Filter.Eq(“Id”,entity.Id);
ReplaceOneResultresult=collection.ReplaceOneAsync(express,old).Result;
if(result.ModifiedCount>0)
{
returntrue;
}
else
{
returnfalse;
}
}
catch(Exceptionex)
{
varaaa=ex.Message+ex.StackTrace;
throw;
}
}
///
///删除
///
///
publicboolDelete(Expression<Func<T,bool>>express)
{
DeleteResultresult=collection.DeleteOneAsync(express).Result;
returnresult.DeletedCount>0?true:false;
}
///
///查询条件查询数据
///
///
publicListQueryAll(Expression<Func<T,bool>>express)
{
returncollection.Find(express).ToList();
}
///
///依据条件查询一条数据
///
///
///
publicTQueryByFirst(Expression<Func<T,bool>>express)
{
returncollection.Find(express).ToList().FirstOrDefault();
}
///
///批量添加
///
///
publicvoidInsertBatch(Listlist)
{
collection.InsertManyAsync(list);
}
///
///依据Id批量删除
///
publicboolDeleteBatch(Listlist)
{
varfilter=Builders.Filter.In(“_id”,list);
DeleteResultresult=collection.DeleteManyAsync(filter).Result;
returnresult.DeletedCount>0?true:false;
}
}
}

未经允许不得转载:IT技术网站 » C#/.Net操作MongoDBHelper类
分享到: 更多 (0)

评论 抢沙发

评论前必须登录!

 

志在指尖 用双手敲打未来

登录/注册IT技术大全

热门IT技术

C#基础入门   SQL server数据库   系统SEO学习教程   WordPress小技巧   WordPress插件   脚本与源码下载