首页 > 代码库 > mongodb
mongodb
public class DoMongo { private static MongoDatabase mdb = MongoCon.mongo; #region 新增 /// <summary> /// 新增 /// </summary> public static Boolean Insert(String collectionName, BsonDocument document) { MongoCollection<BsonDocument> collection = mdb.GetCollection<BsonDocument>(collectionName); try { collection.Insert(document); return true; } catch { return false; } } /// <summary> /// 新增 /// </summary> public static Boolean Insert<T>(String collectionName, T t) { var collection = mdb.GetCollection<T>(collectionName); try { collection.Insert(t); return true; } catch { return false; } } /// <summary> /// 批量新增 /// </summary> public static WriteConcernResult Insert<T>(String collectionName, List<T> list) { var collection = mdb.GetCollection<T>(collectionName); try { return collection.Insert(list); } catch { return null; } } #endregion #region 查询 /// <summary> /// 查询单个对象 /// </summary> public static T GetModel<T>(String collectionName, IMongoQuery query) { MongoCollection<T> collection = mdb.GetCollection<T>(collectionName); try { return collection.FindOneAs<T>(query); } catch { return default(T); } } /// <summary> /// 查询对象集合 /// </summary> public static List<T> GetList<T>(String collectionName, IMongoQuery query) { MongoCollection<T> collection = mdb.GetCollection<T>(collectionName); try { return collection.FindAs<T>(query).ToList(); } catch { return null; } } /// <summary> /// 查询对象集合 /// </summary> public static List<T> GetList<T>(String collectionName, IMongoQuery query, int top) { MongoCollection<T> collection = mdb.GetCollection<T>(collectionName); try { return collection.FindAs<T>(query).SetLimit(top).ToList(); } catch { return null; } } /// <summary> /// 查询对象集合 /// </summary> public static List<T> GetList<T>(String collectionName, IMongoQuery query, string sort, bool isDesc) { MongoCollection<T> collection = mdb.GetCollection<T>(collectionName); try { if (isDesc) { return collection.FindAs<T>(query).SetSortOrder(SortBy.Descending(sort)).ToList(); } else { return collection.FindAs<T>(query).SetSortOrder(SortBy.Ascending(sort)).ToList(); } } catch { return null; } } /// <summary> /// 查询对象集合 /// </summary> public static List<T> GetList<T>(String collectionName, IMongoQuery query, string[] sort, bool isDesc) { MongoCollection<T> collection = mdb.GetCollection<T>(collectionName); try { if (isDesc) { return collection.FindAs<T>(query).SetSortOrder(SortBy.Descending(sort)).ToList(); } else { return collection.FindAs<T>(query).SetSortOrder(SortBy.Ascending(sort)).ToList(); } } catch { return null; } } /// <summary> /// 查询对象集合 /// </summary> public static List<T> GetList<T>(String collectionName, IMongoQuery query, string sort, bool isDesc, int top) { MongoCollection<T> collection = mdb.GetCollection<T>(collectionName); try { if (isDesc) { return collection.FindAs<T>(query).SetSortOrder(SortBy.Descending(sort)).SetLimit(top).ToList(); } else { return collection.FindAs<T>(query).SetSortOrder(SortBy.Ascending(sort)).SetLimit(top).ToList(); } } catch { return null; } } /// <summary> /// 查询对象集合 /// </summary> public static List<T> GetList<T>(String collectionName, IMongoQuery query, string[] sort, bool isDesc, int top) { MongoCollection<T> collection = mdb.GetCollection<T>(collectionName); try { if (isDesc) { return collection.FindAs<T>(query).SetSortOrder(SortBy.Descending(sort)).SetLimit(top).ToList(); } else { return collection.FindAs<T>(query).SetSortOrder(SortBy.Ascending(sort)).SetLimit(top).ToList(); } } catch { return null; } } /// <summary> /// 分页查询 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="collectionName">集合名称</param> /// <param name="query">查询条件</param> /// <param name="sort">排序表达式</param> /// <param name="isDesc">如果为true则为降序,否则为升序</param> /// <param name="index">页索引</param> /// <param name="pageSize">页长度</param> /// <param name="rows">返回多少条数据</param> /// <returns></returns> public static List<T> GetPageList<T>(String collectionName, IMongoQuery query, string sort, bool isDesc, int index, int pageSize, out long rows) { MongoCollection<T> collection = mdb.GetCollection<T>(collectionName); try { rows = collection.FindAs<T>(query).Count(); if (isDesc) { return collection.FindAs<T>(query).SetSortOrder(SortBy.Descending(sort)).SetSkip(index).SetLimit(pageSize).ToList(); } else { return collection.FindAs<T>(query).SetSortOrder(SortBy.Ascending(sort)).SetSkip(index).SetLimit(pageSize).ToList(); } } catch { rows = 0; return null; } } /// <summary> /// 分页查询 /// </summary> public static List<T> GetPageList<T>(String collectionName, IMongoQuery query, string[] sort, bool isDesc, int index, int pageSize, out long rows) { MongoCollection<T> collection = mdb.GetCollection<T>(collectionName); try { rows = collection.FindAs<T>(query).Count(); if (isDesc) { return collection.FindAs<T>(query).SetSortOrder(SortBy.Descending(sort)).SetSkip(index).SetLimit(pageSize).ToList(); } else { return collection.FindAs<T>(query).SetSortOrder(SortBy.Ascending(sort)).SetSkip(index).SetLimit(pageSize).ToList(); } } catch { rows = 0; return null; } } #endregion #region 修改 /// <summary> /// 修改 /// </summary> public static WriteConcernResult Update(String collectionName, IMongoQuery query, QueryDocument update) { MongoCollection<BsonDocument> collection = mdb.GetCollection<BsonDocument>(collectionName); try { var new_doc = new UpdateDocument() { { "$set", update } }; //UpdateFlags设置为Multi时,可批量修改 var result = collection.Update(query, new_doc, UpdateFlags.Multi); return result; } catch { return null; } } #endregion #region 移除 /// <summary> /// 移除匹配的集合 /// </summary> public static Boolean Remove(String collectionName, IMongoQuery query) { MongoCollection<BsonDocument> collection = mdb.GetCollection<BsonDocument>(collectionName); try { collection.Remove(query); return true; } catch { return false; } } /// <summary> /// 移除所有集合 /// </summary> public static Boolean RemoveAll(String collectionName) { MongoCollection<BsonDocument> collection = mdb.GetCollection<BsonDocument>(collectionName); try { collection.RemoveAll(); return true; } catch { return false; } } #endregion #region 其它 /// <summary> /// 是否存在 /// </summary> public static bool IsExist(string collectionName) { MongoCollection<BsonDocument> collection = mdb.GetCollection<BsonDocument>(collectionName); try { return collection.Exists(); } catch { return false; } } /// <summary> /// 总数 /// </summary> public static long Count(string collectionName) { MongoCollection<BsonDocument> collection = mdb.GetCollection<BsonDocument>(collectionName); try { return collection.Count(); } catch { return 0; } } /// <summary> /// 总数 /// </summary> public static long Count(string collectionName, IMongoQuery query) { MongoCollection<BsonDocument> collection = mdb.GetCollection<BsonDocument>(collectionName); try { return collection.Count(query); } catch { return 0; } } #endregion }
public class MongoCon : IDisposable { public static MongoServer mongoCon = null; public static MongoDatabase mongo { get; private set; } private bool disposed = false; static MongoCon() { //创建链接 mongoCon = new MongoServer(MongoConfig.config); //打开链接 mongoCon.Connect(); //获取mongodb指定数据库 mongo = mongoCon.GetDatabase(MongoConfig.mongoDB); } protected virtual void Dispose(bool disposing) { if (!this.disposed) { if (disposing) { //释放链接 mongoCon.Disconnect(); } mongoCon = null; } this.disposed = true; } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } }
public class MongoConfig { public static MongoServerSettings config = null; static MongoConfig() { config = MongoServerSettings.FromUrl(MongoUrl.Create(conStr)); //最大连接池 config.MaxConnectionPoolSize = 500; //最大闲置时间 config.MaxConnectionIdleTime = TimeSpan.FromSeconds(30); //最大存活时间 config.MaxConnectionLifeTime = TimeSpan.FromSeconds(60); //链接时间 config.ConnectTimeout = TimeSpan.FromSeconds(10); //等待队列大小 config.WaitQueueSize = 50; //socket超时时间 config.SocketTimeout = TimeSpan.FromSeconds(10); //队列等待时间 config.WaitQueueTimeout = TimeSpan.FromSeconds(60); //操作时间 config.OperationTimeout = TimeSpan.FromSeconds(60); } public static string conStr { get { return ConfigurationManager.AppSettings["connectionString"]; } } public static string mongoDB { get { return ConfigurationManager.AppSettings["mongoDB"]; } } }
mongodb
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。