C#操作MongoHelp类
using System; using System.Collections.Generic; using System.Linq; using System.Web; using MongoDB.Bson; using MongoDB.Driver; using MongoDB.Driver.Builders; namespace MvcMongoDemo.Common { public class MongoHelper<T> where T:class { private string conn = "mongodb://localhost"; private string dbName = "test"; private string collectionName; //string collection = "user"; private MongoDatabase db; public MongoHelper() { db = GetCollection(); collectionName = typeof(T).Name; } /// <summary> /// 设置你collection /// </summary> private MongoDatabase GetCollection() { MongoClient client = new MongoClient(conn); return client.GetServer().GetDatabase(dbName); } /// <summary> /// 获取所有的数据 /// </summary> /// <returns></returns> public List<T> GetAllList() { return db.GetCollection<T>(collectionName).FindAll().ToList(); } /// <summary> /// 根据条件查询获取数据 /// </summary> /// <param name="query"></param> /// <returns></returns> public List<T> GetList(IMongoQuery query) { return db.GetCollection<T>(collectionName).Find(query).ToList(); } /// <summary> /// 分页查询获取数据信息 /// </summary> /// <param name="query">where条件</param> /// <param name="totalCount">总条数</param> /// <param name="pageIndex">当前页</param> /// <param name="pageSize">每页显示数量</param> /// <returns></returns> public List<T> GetListPage(IMongoQuery query,out int totalCount, int pageIndex=1,int pageSize=10) { totalCount = int.Parse(db.GetCollection<T>(collectionName).Find(query).Count().ToString()); return db.GetCollection<T>(collectionName).Find(query).Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); } /// <summary> /// 根据条件查询返回单条数据 /// </summary> /// <param name="query"></param> /// <returns></returns> public T GetSign(IMongoQuery query) { return db.GetCollection<T>(collectionName).FindOne(query); } /// <summary> /// 根据主键_id查询获取单条数据 /// </summary> /// <param name="id"></param> /// <returns></returns> public T GetSign(BsonValue id) { return db.GetCollection<T>(collectionName).FindOneById(id); } /// <summary> /// 单条数据添加 /// </summary> /// <param name="model"></param> public void Add(T model) { db.GetCollection<T>(collectionName).Insert<T>(model); } /// <summary> /// 批量添加数据 /// </summary> /// <param name="list"></param> public void AddtBatch(List<T> list) { db.GetCollection<T>(collectionName).InsertBatch<T>(list); } /// <summary> /// 根据条件删除数据 /// </summary> /// <param name="query"></param> public void Delete(IMongoQuery query) { db.GetCollection<T>(collectionName).Remove(query); } /// <summary> /// 删除所有数据记录 /// </summary> public void DeleteAll() { db.GetCollection<T>(collectionName).RemoveAll(); } /// <summary> /// 更新单个字段值 /// </summary> /// <param name="query"></param> /// <param name="updateQuery"></param> public void Modity(IMongoQuery query,IMongoUpdate updateQuery ) { db.GetCollection<T>(collectionName).Update(query,updateQuery,UpdateFlags.Multi); } /// <summary> /// 更新多个字段值 /// </summary> /// <param name="query"></param> /// <param name="updates"></param> public void Modity(IMongoQuery query, UpdateBuilder[] updates) { WriteConcernResult result = db.GetCollection<T>(collectionName).Update(query, Update.Combine(updates), UpdateFlags.Multi); } } }
添加数据
Common.MongoHelper<UserInfo> mongoHelp = new Common.MongoHelper<UserInfo>(); List<UserInfo> list = new List<UserInfo>(); for (int i = 1; i <= 5; i++) { UserInfo model = new UserInfo(); model.ID = i; model.Name = "用户" + i; model.Age = 100 - i; model.Gender = i % 3 == 0 ? true : false; model.Address = "地址" + i; model.AddTime = System.DateTime.Now.AddDays(-i); //单条数据记录添加 // mongoHelp.Add(model); list.Add(model); } //批量插入 // mongoHelp.AddtBatch(list);
删除数据
IMongoQuery query = null; query = Query.And( Query.EQ("Name", "用户2") ); //根据条件删除 mongoHelp.Delete(query); //删除所有数据 mongoHelp.DeleteAll();
更新数据
IMongoQuery queryWhere = Query.And(Query.EQ("ID",1)); List<UpdateBuilder> updateList = new List<UpdateBuilder>(); updateList.Add(Update.Set("Address", "我现在住在北京哈哈```")); updateList.Add(Update.Set("Name", "用户修改过的1")); updateList.Add(Update.Set("Age", 1002)); IMongoUpdate update = Update.Set("Name", "用户修改过的"); //对单个字段值进行更新 mongoHelp.Modity(queryWhere, update); //更新多个字段值 mongoHelp.Modity(queryWhere, updateList.ToArray());
查询数据
//根据_id索引去查询单条数据 UserInfo model2=null; BsonValue id = "5973374e718e126c2801b81d"; model2= mongoHelp.GetSign(id); //根据条件去查询数据 model2 = mongoHelp.GetSign(queryWhere); List<UserInfo> listModel = new List<UserInfo>(); //查询返回多条数据记录 listModel = mongoHelp.GetList(queryWhere); //获取所有数据记录 listModel = mongoHelp.GetAllList(); //分页获取数据 int totalCount = 0; listModel = mongoHelp.GetListPage(queryWhere, out totalCount, 1, 10);
UserInfo实体类
public class UserInfo { /// <summary> /// 主键索引,没有该字段更新数据时会报异常 /// </summary> public ObjectId _id { get; set; } public int ID { get; set; } public string Name { get; set; } public int Age { get; set; } public bool Gender { get; set; } public string Address { get; set; } public DateTime AddTime { get; set; } }
时间: 2024-11-14 07:48:52