先 nuget两个程序集 1:mongodb.driver、   2:mongodb.bson

namespace consoleapp1
{
/// <summary>
/// mongodb帮助类
/// </summary>
/// <summary>
/// mongodb帮助类
/// </summary>
public class db
{
private static readonly string connstr = “mongodb://127.0.0.1:27017”;//globalconfig.settings[“mongoconnstr”];

private static readonly string dbname = “school”;//globalconfig.settings[“mongodbname”];

private static imongodatabase db = null;

private static readonly object lockhelper = new object();

private db() { }

public static imongodatabase getdb()
{
if (db == null)
{
lock (lockhelper)
{
if (db == null)
{
var client = new mongoclient(connstr);
db = client.getdatabase(dbname);
}
}
}
return db;
}
}

public class mongodbhelper<t>
{
private imongodatabase db = null;

private imongocollection<t> collection = null;

public mongodbhelper()
{
this.db = db.getdb();
collection = db.getcollection<t>(typeof(t).name);
}
/// <summary>
/// 新增
/// </summary>
/// <param name=”entity”></param>
/// <returns></returns>
public t insert(t entity)
{
var flag = objectid.generatenewid();
entity.gettype().getproperty(“_id”).setvalue(entity, flag);
collection.insertoneasync(entity);
return entity;
}

/// <summary>
/// 修改一个值
/// </summary>
/// <param name=”express”></param>
/// <param name=”field”></param>
/// <param name=”value”></param>
public bool modify(expression<func<t, bool>> express, object updatefield)
{
if (updatefield == null) return false;

var props = updatefield.gettype().getproperties();
var field = props[0].name;
var value = props[0].getvalue(updatefield);
var updated = builders<t>.update.set(field, value);

updateresult result = collection.updateoneasync(express, updated).result;
return result.modifiedcount > 0 ? true : false;
}

 

/// <summary>
/// 更新
/// </summary>
/// <param name=”entity”></param>
public bool update(expression<func<t, bool>> express, t entity)
{
try
{
var old = collection.find(express).tolist().firstordefault();

foreach (var prop in entity.gettype().getproperties())
{
if (!prop.name.equals(“_id”))
{
var newvalue = prop.getvalue(entity);
var oldvalue = 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());
}
}
}
}

// var filter = builders<t>.filter.eq(“id”, entity.id);
replaceoneresult result = collection.replaceoneasync(express, old).result;
if (result.modifiedcount > 0)
{
return true;
}
else
{
return false;
}
}
catch (exception ex)
{
var aaa = ex.message + ex.stacktrace;
throw;
}
}

/// <summary>
/// 删除
/// </summary>
/// <param name=”entity”></param>
public bool delete(expression<func<t, bool>> express)
{
deleteresult result = collection.deleteoneasync(express).result;
return result.deletedcount > 0 ? true : false;
}

 

/// <summary>
/// 查询条件查询数据
/// </summary>
/// <returns></returns>
public list<t> queryall(expression<func<t, bool>> express)
{
return collection.find(express).tolist();
}

/// <summary>
/// 根据条件查询一条数据
/// </summary>
/// <param name=”express”></param>
/// <returns></returns>
public t querybyfirst(expression<func<t, bool>> express)
{
return collection.find(express).tolist().firstordefault();
}

 

/// <summary>
/// 批量添加
/// </summary>
/// <param name=”list”></param>
public void insertbatch(list<t> list)
{
collection.insertmanyasync(list);
}

/// <summary>
/// 根据id批量删除
/// </summary>
public bool deletebatch(list<objectid> list)
{
var filter = builders<t>.filter.in(“_id”, list);
deleteresult result = collection.deletemanyasync(filter).result;
return result.deletedcount > 0 ? true : false;
}

}

 

}