Exemple #1
0
        /// <summary>
        /// 根据文档名称、查询条件获取文档指定列的数据
        /// </summary>
        /// <typeparam name="T">文档类型</typeparam>
        /// <param name="colName">文档名称</param>
        /// <param name="query">查询条件</param>
        /// <param name="fields">列列表</param>
        /// <returns>文档符合条件所有数据</returns>
        public static List <T> Get <T>(string colName, IMongoQuery query, IMongoFields fields)
        {
            var mongo      = MongoManager.getDB();
            var collection = mongo.GetCollection <T>(colName);

            return(collection.FindAs <T>(query).SetFields(fields).ToList());
        }
Exemple #2
0
        /// <summary>
        /// 保存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="colName"></param>
        /// <param name="obj"></param>
        public static void Save <T>(string colName, T obj)
        {
            var mongo      = MongoManager.getDB();
            var collection = mongo.GetCollection(colName);

            collection.Save(typeof(T), obj);
        }
Exemple #3
0
        /// <summary>
        /// 根据文档名称更新指定内容
        /// </summary>
        /// <param name="colName">文档名称</param>
        /// <param name="query">查询条件</param>
        /// <param name="update">更新内容</param>
        /// <param name="updateFlags">更新类型(upsert:如果不存在插入)</param>
        /// <typeparam name="T"></typeparam>
        public static void Update <T>(string colName, IMongoQuery query, IMongoUpdate update, UpdateFlags updateFlags)
        {
            var mongo      = MongoManager.getDB();
            var collection = mongo.GetCollection <T>(colName);

            collection.Update(query, update, updateFlags);
        }
Exemple #4
0
        /// <summary>
        /// 根据文档名称更新指定内容
        /// </summary>
        /// <param name="colName">文档名称</param>
        /// <param name="query">查询条件</param>
        /// <param name="update">更新内容</param>
        /// <typeparam name="T">文档类型</typeparam>
        /// <returns></returns>
        public static bool Update <T>(string colName, IMongoQuery query, IMongoUpdate update)
        {
            var mongo      = MongoManager.getDB();
            var collection = mongo.GetCollection <T>(colName);

            var result = collection.Update(query, update);

            return(result.Ok);
        }
Exemple #5
0
        /// <summary>
        /// 保存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="colName"></param>
        /// <param name="list"></param>
        public static void Save <T>(string colName, IEnumerable <T> list)
        {
            var enumerable = list as T[] ?? list.ToArray();

            if (list != null && enumerable.Any())
            {
                var mongo      = MongoManager.getDB();
                var collection = mongo.GetCollection(colName);
                foreach (var item in enumerable)
                {
                    collection.Save(item);
                }
            }
        }