/// <summary> /// Insert New Document /// </summary> /// <typeparam name="T">Entity "T"</typeparam> /// <param name="o"></param> /// <returns>newdocument'id</returns> public int Insert <T>(T o) { try { Type t = typeof(T); MongoCollection <BsonDocument> coll = BaseDB.CreateInstance().GetCollection(t.Name); int seq = BaseDB.GetSeqence("Seqence" + t.Name); o.GetType().GetProperty("_id").SetValue(o, seq, null); coll.Insert(o); return(seq); } catch { return(0); } finally { BaseDB.MongoService.Disconnect(); } }
/// <summary> /// /// </summary> /// <typeparam name="T"></typeparam> /// <param name="compColumnName">公司字段名称</param> /// <param name="isContainDeleted"></param> /// <param name="TenantId"></param> /// <returns></returns> public List <T> GetAllListByCompanyId <T>(string compColumnName, int TenantId = 0, bool isContainDeleted = false) { try { MongoCollection <T> coll = BaseDB.CreateInstance().GetCollection <T>(typeof(T).Name); return(isContainDeleted ? coll.Find(Query.EQ(compColumnName, TenantId)).ToList() : coll.Find(Query.And(new[] { Query.EQ(compColumnName, TenantId), Query.EQ("Status", 0) })).ToList()); } catch { return(new List <T>()); } finally { BaseDB.MongoService.Disconnect(); } }
/// <summary> /// Delete Entity By ID /// </summary> /// <typeparam name="T">Entity"T"</typeparam> /// <param name="id">ID</param> /// <param name="isTrueDelete">true:delete true,false:delete false</param> /// <returns></returns> public bool Delete <T>(int id, bool isTrueDelete) { try { MongoCollection <T> coll = BaseDB.CreateInstance().GetCollection <T>(typeof(T).Name); if (isTrueDelete) { coll.Remove(Query.EQ("_id", id), SafeMode.True); } else { coll.Update(Query.EQ("_id", id), Update.Set("Status", 1), UpdateFlags.Multi, SafeMode.True); } return(true); } catch { return(false); } finally { BaseDB.MongoService.Disconnect(); } }
/// <summary> /// Insert New Document Collectionss /// </summary> /// <typeparam name="T">Entity Collections "T"</typeparam> /// <param name="os"></param> /// <returns></returns> public bool Insert <T>(IEnumerable <T> os) { try { Type t = typeof(T); MongoCollection <BsonDocument> coll = BaseDB.CreateInstance().GetCollection(t.Name); foreach (T o in os) { int seq = BaseDB.GetSeqence("Seqence" + t.Name); o.GetType().GetProperty("_id").SetValue(o, seq, null); coll.Insert(o); } return(true); } catch { return(false); } finally { BaseDB.MongoService.Disconnect(); } }
/// <summary> /// Get Entity List(paging、sorting) /// </summary> /// <typeparam name="T">Entity</typeparam> /// <param name="totalCount">data'count</param> /// <param name="query">QueryConditions</param> /// <param name="sort">SortConditions</param> /// <param name="pageIndex">PageIndex</param> /// <param name="pageSize">PageSize</param> /// <returns></returns> public List <T> GetAllList <T>(ref int totalCount, IMongoQuery query = null, IMongoSortBy sort = null, int pageIndex = 0, int pageSize = 0) { try { MongoCollection <T> collection = BaseDB.CreateInstance().GetCollection <T>(typeof(T).Name); sort = sort ?? new SortByDocument(); pageSize = (pageSize == 0) ? 1 : pageSize; totalCount = (int) (query == null ? collection.Find(Query.EQ("Status", 0)).Count() : collection.Find(Query.And(query, Query.EQ("Status", 0))).Count()); if (pageIndex < 1) { return(((query == null) ? collection.FindAll() : collection.Find(query)).SetSortOrder(sort).ToList()); } else { return (((query == null) ? collection.Find(Query.EQ("Status", 0)) : collection.Find(Query.And(query, Query.EQ("Status", 0)))).SetSortOrder(sort).SetSkip( (pageIndex - 1) * pageSize).SetLimit(pageSize).ToList()); } } catch { return(new List <T>()); } finally { BaseDB.MongoService.Disconnect(); } }