Пример #1
0
        /// <summary>
        /// 获取文件流
        /// </summary>
        /// <param name="host"></param>
        /// <param name="id">文件id,fs.files中的_id,也对应fs.chunks的files_id</param>
        /// <param name="options">下载选项</param>
        public static Stream DownloadFileToStream(MongodbHost host, BsonValue id, GridFSDownloadOptions options = null)
        {
            Stream destination = new MemoryStream();
            var    bucket      = MongodbManager <GridFSBucket> .GetGridFSBucket(host);

            bucket.DownloadToStream(id, destination, options);
            return(destination);
        }
Пример #2
0
        /// <summary>
        /// 上传文件流,并附上元数据
        /// </summary>
        /// <param name="host"></param>
        /// <param name="fileName"></param>
        /// <param name="source"></param>
        /// <param name="metadata"></param>
        /// <returns></returns>
        public static ObjectId UploadFileFromStream(MongodbHost host, string fileName, Stream source, IDictionary <string, object> metadata)
        {
            var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(host);

            GridFSUploadOptions options = new GridFSUploadOptions
            {
                Metadata = new BsonDocument(metadata)
            };

            return(bucket.UploadFromStream(fileName, source, options));
        }
Пример #3
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="host"></param>
        /// <param name="filePath">文件完整路径</param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static ObjectId UploadFileFromPath(MongodbHost host, string filePath, GridFSUploadOptions options = null)
        {
            if (!File.Exists(filePath))
            {
                throw new Exception($"{filePath}  does not exist");
            }
            var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(host);

            FileStream fileStream = new FileStream(filePath, FileMode.Open);

            return(bucket.UploadFromStream(Path.GetFileName(filePath), fileStream, options));
        }
Пример #4
0
        /// <summary>
        /// 批量删除数据
        /// </summary>
        /// <param name="host">mongodb连接信息</param>
        /// <param name="filter">删除的条件</param>
        /// <returns></returns>
        public static DeleteResult DeleteMany(MongodbHost host, FilterDefinition <T> filter)
        {
            try
            {
                var client = MongodbManager <T> .GetMongodbCollection(host);

                return(client.DeleteMany(filter));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Пример #5
0
        /// <summary>
        /// 批量删除数据
        /// </summary>
        /// <param name="database"></param>
        /// <param name="filter"></param>
        /// <returns></returns>
        public static DeleteResult DeleteMany(IMongoDatabase database, FilterDefinition <T> filter)
        {
            try
            {
                var client = MongodbManager <T> .GetMongodbCollection(database);

                return(client.DeleteMany(filter));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Пример #6
0
        public static async Task <DeleteResult> DeleteManyAsync(IMongoDatabase host, FilterDefinition <T> filter)
        {
            try
            {
                var client = MongodbManager <T> .GetMongodbCollection(host);

                return(await client.DeleteManyAsync(filter));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Пример #7
0
        /// <summary>
        /// 根据条件获取总数
        /// </summary>
        /// <param name="host">mongodb连接信息</param>
        /// <param name="filter">条件</param>
        /// <returns></returns>
        public static long Count(MongodbHost host, FilterDefinition <T> filter)
        {
            try
            {
                var client = MongodbManager <T> .GetMongodbCollection(host);

                return(client.CountDocuments(filter));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Пример #8
0
        /// <summary>
        /// 异步根据条件获取总数
        /// </summary>
        /// <param name="host">mongodb连接信息</param>
        /// <param name="filter">条件</param>
        /// <returns></returns>
        public static async Task <long> CountAsync(MongodbHost host, FilterDefinition <T> filter)
        {
            try
            {
                var client = MongodbManager <T> .GetMongodbCollection(host);

                return(await client.CountAsync(filter));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Пример #9
0
        /// <summary>
        /// 添加一条数据
        /// </summary>
        /// <param name="t">添加的实体</param>
        /// <param name="host">mongodb连接信息</param>
        /// <returns></returns>
        public static int Add(MongodbHost host, T t)
        {
            try
            {
                var client = MongodbManager <T> .GetMongodbCollection(host);

                client.InsertOne(t);
                return(1);
            }
            catch
            {
                return(0);
            }
        }
Пример #10
0
        /// <summary>
        /// 重命名所有匹配的文件
        /// </summary>
        /// <param name="host"></param>
        /// <param name="oldFilename"></param>
        /// <param name="newFilename"></param>
        public static void RenameAllfile(MongodbHost host, string oldFilename, string newFilename)
        {
            var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(host);

            var filter = Builders <GridFSFileInfo> .Filter.Eq(x => x.Filename, oldFilename);

            var filesCursor = bucket.Find(filter);
            var files       = filesCursor.ToList();

            foreach (var file in files)
            {
                bucket.Rename(file.Id, newFilename);
            }
        }
Пример #11
0
        /// <summary>
        /// 插入记录
        /// </summary>
        /// <param name="mongoDataBase"></param>
        /// <param name="t"></param>
        /// <returns></returns>
        public static int Add(IMongoDatabase mongoDataBase, T t)
        {
            try
            {
                var collection = MongodbManager <T> .GetMongodbCollection(mongoDataBase);

                collection.InsertOne(t);
                return(1);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Пример #12
0
        /// <summary>
        /// 异步添加一条数据
        /// </summary>
        /// <param name="t">添加的实体</param>
        /// <param name="host">mongodb连接信息</param>
        /// <returns></returns>
        public static async Task <int> AddAsync(MongodbHost host, T t)
        {
            try
            {
                var client = MongodbManager <T> .GetMongodbCollection(host);

                await client.InsertOneAsync(t);

                return(1);
            }
            catch
            {
                return(0);
            }
        }
Пример #13
0
        /// <summary>
        /// 批量插入
        /// </summary>
        /// <param name="host">mongodb连接信息</param>
        /// <param name="t">实体集合</param>
        /// <returns></returns>
        public static int InsertMany(MongodbHost host, List <T> t)
        {
            try
            {
                var client = MongodbManager <T> .GetMongodbCollection(host);

                client.InsertMany(t);
                return(1);
            }
            catch (Exception ex)
            {
                LOG.Error(ex);
                return(0);
            }
        }
Пример #14
0
        /// <summary>
        /// 删除一条数据
        /// </summary>
        /// <param name="host">mongodb连接信息</param>
        /// <param name="id">objectId</param>
        /// <returns></returns>
        public static DeleteResult Delete(MongodbHost host, string id)
        {
            try
            {
                var client = MongodbManager <T> .GetMongodbCollection(host);

                FilterDefinition <T> filter = Builders <T> .Filter.Eq("_id", new ObjectId(id));

                return(client.DeleteOne(filter));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Пример #15
0
        /// <summary>
        /// 异步删除一条数据
        /// </summary>
        /// <param name="host">mongodb连接信息</param>
        /// <param name="id">objectId</param>
        /// <returns></returns>
        public static async Task <DeleteResult> DeleteAsync(MongodbHost host, string id)
        {
            try
            {
                var client = MongodbManager <T> .GetMongodbCollection(host);

                //修改条件
                FilterDefinition <T> filter = Builders <T> .Filter.Eq("_id", new ObjectId(id));

                return(await client.DeleteOneAsync(filter));
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Пример #16
0
        /// <summary>
        /// 从本地路径上传文件
        /// </summary>
        /// <param name="host"></param>
        /// <param name="filePath"></param>
        /// <param name="metadata"></param>
        /// <returns></returns>
        public static ObjectId UploadFileFromPath(MongodbHost host, string filePath, IDictionary <string, object> metadata)
        {
            if (!File.Exists(filePath))
            {
                throw new Exception($"{filePath}  does not exist");
            }
            var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(host);

            GridFSUploadOptions options = new GridFSUploadOptions
            {
                Metadata = new BsonDocument(metadata)
            };
            FileStream fileStream = new FileStream(filePath, FileMode.Open);

            return(bucket.UploadFromStream(fileStream.Name, fileStream, options));
        }
Пример #17
0
        /// <summary>
        /// 根据文件名查找所有匹配的文件
        /// </summary>
        /// <param name="host">mongo服务器信息</param>
        /// <param name="fileName">文件名称</param>
        /// <returns></returns>
        public static IAsyncCursor <GridFSFileInfo> FindFiles(MongodbHost host, string fileName)
        {
            var filter = Builders <GridFSFileInfo> .Filter.And(
                Builders <GridFSFileInfo> .Filter.Eq(x => x.Filename, fileName)
                // Builders<GridFSFileInfo>.Filter.Eq("metadata.UserID", fileName)
                );

            var sort = Builders <GridFSFileInfo> .Sort.Descending(x => x.UploadDateTime);

            var options = new GridFSFindOptions
            {
                // Limit = 1,
                Sort = sort
            };
            var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(host);

            return(bucket.Find(filter, options));
        }
Пример #18
0
        /// <summary>
        /// 根据元数据查找文件集合
        /// </summary>
        /// <param name="host"></param>
        /// <param name="metadatas"></param>
        /// <returns></returns>
        public static IAsyncCursor <GridFSFileInfo> FindFiles(MongodbHost host, IDictionary <string, object> metadatas)
        {
            IList <FilterDefinition <GridFSFileInfo> > filters = new List <FilterDefinition <GridFSFileInfo> >();

            foreach (var item in metadatas)
            {
                filters.Add(Builders <GridFSFileInfo> .Filter.Eq(item.Key, item.Value));
            }
            var sort = Builders <GridFSFileInfo> .Sort.Descending(x => x.UploadDateTime);

            var options = new GridFSFindOptions
            {
                Limit = 1,
                Sort  = sort
            };
            var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(host);

            return(bucket.Find(Builders <GridFSFileInfo> .Filter.And(filters), options));
        }
Пример #19
0
        /// <summary>
        /// 分页查询集合
        /// </summary>
        /// <param name="host">mongodb连接信息</param>
        /// <param name="filter">查询条件</param>
        /// <param name="pageIndex">当前页</param>
        /// <param name="pageSize">页容量</param>
        /// <param name="count">总条数</param>
        /// <param name="field">要查询的字段,不写时查询全部</param>
        /// <param name="sort">要排序的字段</param>
        /// <returns></returns>
        public static List <T> FindListByPage(MongodbHost host, FilterDefinition <T> filter, int pageIndex, int pageSize, out long count, string[] field = null, SortDefinition <T> sort = null)
        {
            try
            {
                var client = MongodbManager <T> .GetMongodbCollection(host);

                count = client.CountDocuments(filter);
                //不指定查询字段
                if (field == null || field.Length == 0)
                {
                    if (sort == null)
                    {
                        return(client.Find(filter).Skip((pageIndex - 1) * pageSize).Limit(pageSize).ToList());
                    }
                    //进行排序
                    return(client.Find(filter).Sort(sort).Skip((pageIndex - 1) * pageSize).Limit(pageSize).ToList());
                }

                //制定查询字段
                var fieldList = new List <ProjectionDefinition <T> >();
                for (int i = 0; i < field.Length; i++)
                {
                    fieldList.Add(Builders <T> .Projection.Include(field[i].ToString()));
                }
                var projection = Builders <T> .Projection.Combine(fieldList);

                fieldList?.Clear();

                //不排序
                if (sort == null)
                {
                    return(client.Find(filter).Project <T>(projection).Skip((pageIndex - 1) * pageSize).Limit(pageSize).ToList());
                }

                //排序查询
                return(client.Find(filter).Sort(sort).Project <T>(projection).Skip((pageIndex - 1) * pageSize).Limit(pageSize).ToList());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Пример #20
0
        /// <summary>
        /// 查找唯一文件
        /// </summary>
        /// <param name="database"></param>
        /// <param name="metadatas"></param>
        /// <returns></returns>
        public static GridFSFileInfo FindSingleFile(IMongoDatabase database, IDictionary <string, object> metadatas)
        {
            IList <FilterDefinition <GridFSFileInfo> > filters = new List <FilterDefinition <GridFSFileInfo> >();

            foreach (var item in metadatas)
            {
                filters.Add(Builders <GridFSFileInfo> .Filter.Eq(item.Key, item.Value));
            }
            var sort = Builders <GridFSFileInfo> .Sort.Descending(x => x.UploadDateTime);

            var options = new GridFSFindOptions
            {
                Limit = 1,
                Sort  = sort
            };
            var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(database);

            using (var cursor = bucket.Find(Builders <GridFSFileInfo> .Filter.And(filters), options))
            {
                return(cursor.ToList().FirstOrDefault());
            }
        }
Пример #21
0
        public static List <T> FindList(IMongoDatabase database, FilterDefinition <T> filter, string[] field = null, SortDefinition <T> sort = null)
        {
            try
            {
                var client = MongodbManager <T> .GetMongodbCollection(database);

                //不指定查询字段
                if (field == null || field.Length == 0)
                {
                    if (sort == null)
                    {
                        return(client.Find(filter).ToList());
                    }
                    //进行排序
                    return(client.Find(filter).Sort(sort).ToList());
                }

                //制定查询字段
                var fieldList = new List <ProjectionDefinition <T> >();
                for (int i = 0; i < field.Length; i++)
                {
                    fieldList.Add(Builders <T> .Projection.Include(field[i].ToString()));
                }
                var projection = Builders <T> .Projection.Combine(fieldList);

                fieldList?.Clear();
                if (sort == null)
                {
                    return(client.Find(filter).Project <T>(projection).ToList());
                }
                //排序查询
                return(client.Find(filter).Sort(sort).Project <T>(projection).ToList());
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Пример #22
0
        /// <summary>
        /// 根据文件名下载文件字节数组
        /// </summary>
        /// <param name="host"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static Byte[] DownloadFileAsBytesByName(MongodbHost host, string fileName)
        {
            var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(host);

            return(bucket.DownloadAsBytesByName(fileName));
        }
Пример #23
0
        /// <summary>
        /// 同步上传
        /// </summary>
        /// <param name="database"></param>
        /// <param name="fileName"></param>
        /// <param name="source"></param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static ObjectId UploadFileFromStream(IMongoDatabase database, string fileName, Stream source, GridFSUploadOptions options = null)
        {
            var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(database);

            return(bucket.UploadFromStream(fileName, source, options));
        }
Пример #24
0
        /// <summary>
        /// 获取文件字节数组
        /// </summary>
        /// <param name="database"></param>
        /// <param name="fileName"></param>
        /// <returns></returns>
        public static Byte[] DownloadFileAsBytesByName(IMongoDatabase database, string fileName)
        {
            var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(database);

            return(bucket.DownloadAsBytesByName(fileName));
        }
Пример #25
0
        /// <summary>
        /// 异步通过字节流上传文件
        /// </summary>
        /// <param name="host"></param>
        /// <param name="fileName">文件名</param>
        /// <param name="source">文件字节</param>
        /// <param name="options"></param>
        /// <returns></returns>
        public static Task <ObjectId> UploadFileFromBytesAsync(MongodbHost host, string fileName, byte[] source, GridFSUploadOptions options = null)
        {
            var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(host);

            return(bucket.UploadFromBytesAsync(fileName, source, options));
        }
Пример #26
0
        /// <summary>
        /// 查找操作
        /// </summary>
        /// <param name="host">mongodb服务器信息</param>
        /// <param name="filter">查询过滤器</param>
        /// <param name="options">查找选项</param>
        /// <returns></returns>
        public static IAsyncCursor <GridFSFileInfo> FindFiles(MongodbHost host, FilterDefinition <GridFSFileInfo> filter, GridFSFindOptions options = null)
        {
            var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(host);

            return(bucket.Find(filter, options));
        }
Пример #27
0
        /// <summary>
        /// 重命名
        /// </summary>
        /// <param name="host">mongo服务器信息</param>
        /// <param name="id">文件id,fs.files中的_id,也对应fs.chunks的files_id</param>
        /// <param name="newFileName">新文件名称</param>
        public static void RenameFile(MongodbHost host, BsonValue id, string newFileName)
        {
            var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(host);

            bucket.Rename(id, newFileName);
        }
Пример #28
0
        /// <summary>
        /// 根据ObjectId删除文件
        /// </summary>
        /// <param name="database"></param>
        /// <param name="id"></param>
        public static void DeleteFileById(IMongoDatabase database, ObjectId id)
        {
            var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(database);

            bucket.Delete(id);
        }
Пример #29
0
        /// <summary>
        /// 根据ObjectId删除文件
        /// </summary>
        /// <param name="host"></param>
        /// <param name="id">ObjectId</param>
        public static void DeleteFileById(MongodbHost host, ObjectId id)
        {
            var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(host);

            bucket.Delete(id);
        }
Пример #30
0
        /// <summary>
        /// 删除fs数据库
        /// </summary>
        /// <param name="host"></param>
        public static void DropFS(MongodbHost host)
        {
            var bucket = MongodbManager <GridFSBucket> .GetGridFSBucket(host);

            bucket.Drop();
        }