c#操作mongodb
最近有点忙,换工作了,也加入到996的阵营中,一个字,累啊,哈哈,为自己加油。
1.环境:
windows10
vs2015
2.下载驱动程序
https://github.com/mongodb/mongo-csharp-driver/downloads
当然我们会使用NuGet
3.帮助类
DB.cs
public class DB { private static readonly string connStr = "mongodb://127.0.0.1:27017"; private static readonly string dbName = "mytestdb"; 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; } }
MongoDbHelper类
public class MongoDbHelper<T> where T : BaseEntity { 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); entity.State = "y"; entity.CreateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); entity.UpdateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); collection.InsertOneAsync(entity); return entity; } /// <summary> /// 修改 /// </summary> /// <param name="id"></param> /// <param name="field"></param> /// <param name="value"></param> public void Modify(string id, string field, string value) { var filter = Builders<T>.Filter.Eq("Id", ObjectId.Parse(id)); var updated = Builders<T>.Update.Set(field, value); UpdateResult result = collection.UpdateOneAsync(filter, updated).Result; } /// <summary> /// 更新 /// </summary> /// <param name="entity"></param> public void Update(T entity) { try { var old = collection.Find(e => e.Id.Equals(entity.Id)).ToList().FirstOrDefault(); foreach (var prop in entity.GetType().GetProperties()) { 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()); } } } old.State = "n"; old.UpdateTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); var filter = Builders<T>.Filter.Eq("Id", entity.Id); ReplaceOneResult result = collection.ReplaceOneAsync(filter, old).Result; } catch (Exception ex) { var aaa = ex.Message + ex.StackTrace; throw; } } /// <summary> /// 删除 /// </summary> /// <param name="entity"></param> public void Delete(T entity) { var filter = Builders<T>.Filter.Eq("Id", entity.Id); collection.DeleteOneAsync(filter); } /// <summary> /// 根据id查询一条数据 /// </summary> /// <param name="id"></param> /// <returns></returns> public T QueryOne(string id) { return collection.Find(a => a.Id == ObjectId.Parse(id)).ToList().FirstOrDefault(); } /// <summary> /// 查询所有数据 /// </summary> /// <returns></returns> public List<T> QueryAll() { return collection.Find(a => a.State != "").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 void DeleteBatch(List<ObjectId> list) { var filter = Builders<T>.Filter.In("Id", list); collection.DeleteManyAsync(filter); } /// <summary> /// 未添加到索引的数据 /// </summary> /// <returns></returns> public List<T> QueryToLucene() { return collection.Find(a => a.State.Equals("y") || a.State.Equals("n")).ToList(); } }
BaseEntity类
public abstract class BaseEntity { public ObjectId Id { get; set; } public string State { get; set; } public string CreateTime { get; set; } public string UpdateTime { get; set; } }
mongoCustomer类
public class mongoCustomer : BaseEntity { /// <summary> /// 名称 /// </summary> public string Name { get; set; } /// <summary> /// 传真 /// </summary> public string TaxCode { get; set; } /// <summary> /// 电话号 /// </summary> public string Phone { get; set; } }
4.测试
class Program { static void Main(string[] args) { mongodbtest.MongoDbHelper<mongoCustomer> helper = new MongoDbHelper<mongoCustomer>(); mongoCustomer m = new mongodbtest.mongoCustomer(); m.Phone = "15010379999"; m.CreateTime = DateTime.Now.ToString(); m.UpdateTime = DateTime.Now.ToString(); m.Name = "小王"; m.State = "State"; m.TaxCode = "007"; helper.Insert(m); var list= helper.QueryAll(); foreach (var item in list) { Console.WriteLine(item.Name); } Console.ReadLine(); } }