C#/.Net操作MongoDBHelper类

先 NuGet两个程序集 1:MongoDB.Driver、 2:MongoDB.Bson

namespace ConsoleApp1
{
///
/// MongoDb帮助类
///
///
/// MongoDb帮助类
///
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
{
private IMongoDatabase db = null;

private IMongoCollection collection = null;

public MongoDbHelper()
{
this.db = DB.GetDb();
collection = db.GetCollection(typeof(T).Name);
}
///
/// 新增
///
///
///
public T Insert(T entity)
{
var flag = ObjectId.GenerateNewId();
entity.GetType().GetProperty("_id").SetValue(entity, flag);
collection.InsertOneAsync(entity);
return entity;
}

///
/// 修改一个值
///
///
///
///
public bool Modify(Expression> 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.Update.Set(field, value);

UpdateResult result = collection.UpdateOneAsync(express, updated).Result;
return result.ModifiedCount > 0 ? true : false;
}

///
/// 更新
///
///
public bool Update(Expression> 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.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;
}
}

///
/// 删除
///
///
public bool Delete(Expression> express)
{
DeleteResult result = collection.DeleteOneAsync(express).Result;
return result.DeletedCount > 0 ? true : false;
}

///
/// 查询条件查询数据
///
///
public List QueryAll(Expression> express)
{
return collection.Find(express).ToList();
}

///
/// 根据条件查询一条数据
///
///
///
public T QueryByFirst(Expression> express)
{
return collection.Find(express).ToList().FirstOrDefault();
}

///
/// 批量添加
///
///
public void InsertBatch(List list)
{
collection.InsertManyAsync(list);
}

///
/// 根据Id批量删除
///
public bool DeleteBatch(List list)
{
var filter = Builders.Filter.In("_id", list);
DeleteResult result = collection.DeleteManyAsync(filter).Result;
return result.DeletedCount > 0 ? true : false;
}

}

}

你可能感兴趣的:(随笔)