C#/

IT技术2年前 (2022)更新 投稿用户
0

先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;
}
}
}

© 版权声明
好牛新坐标 广告
版权声明:
1、IT大王遵守相关法律法规,由于本站资源全部来源于网络程序/投稿,故资源量太大无法一一准确核实资源侵权的真实性;
2、出于传递信息之目的,故IT大王可能会误刊发损害或影响您的合法权益,请您积极与我们联系处理(所有内容不代表本站观点与立场);
3、因时间、精力有限,我们无法一一核实每一条消息的真实性,但我们会在发布之前尽最大努力来核实这些信息;
4、无论出于何种目的要求本站删除内容,您均需要提供根据国家版权局发布的示范格式
《要求删除或断开链接侵权网络内容的通知》:https://itdw.cn/ziliao/sfgs.pdf,
国家知识产权局《要求删除或断开链接侵权网络内容的通知》填写说明: http://www.ncac.gov.cn/chinacopyright/contents/12227/342400.shtml
未按照国家知识产权局格式通知一律不予处理;请按照此通知格式填写发至本站的邮箱 wl6@163.com

相关文章