• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase
您的位置:首页 > 数据库 >MongoDB > MongoDB.NET 2.2.4驱动版本对Mongodb3.3数据库中GridFS增删改查

MongoDB.NET 2.2.4驱动版本对Mongodb3.3数据库中GridFS增删改查

作者:天风隼 字体:[增加 减小] 来源:互联网 时间:2017-05-11

天风隼通过本文主要向大家介绍了mongodb gridfs,mongodb gridfs java,未转变者2.2.4,未转变者2.2.4外挂,未转变者2.2.4修改器等相关知识,希望本文的分享对您有所帮助

本文实例为大家分享了针对Mongodb3.3数据库中GridFS增删改查,供大家参考,具体内容如下

Program.cs代码如下:

internal class Program 
 { 
  private static void Main(string[] args) 
  { 
   GridFSHelper helper = new GridFSHelper("mongodb://localhost", "GridFSDemo", "Pictures"); 
 
   #region 上传图片 
 
   //第一种 
   //Image image = Image.FromFile("D:\\dog.jpg"); 
   //byte[] imgdata = ImageHelper.ImageToBytes(image); 
   //ObjectId oid = helper.UploadGridFSFromBytes(imgdata); 
 
   //第二种 
   //Image image = Image.FromFile("D:\\man.jpg"); 
   //Stream imgSteam = ImageHelper.ImageToStream(image); 
   //ObjectId oid = helper.UploadGridFSFromStream("man",imgSteam); 
   //LogHelper.WriteFile(oid.ToString()); 
   // Console.Write(oid.ToString()); 
 
   #endregion 
 
   #region 下载图片 
 
   //第一种 
   //ObjectId downId = new ObjectId("578e2d17d22aed1850c7855d"); 
   //byte[] Downdata= helper.DownloadAsByteArray(downId); 
   //string name= ImageHelper.CreateImageFromBytes("coolcar",Downdata); 
 
   //第二种 
   // byte[] Downdata = helper.DownloadAsBytesByName("QQQ"); 
   //string name = ImageHelper.CreateImageFromBytes("dog", Downdata); 
 
   //第三种 
   //byte[] Downdata = helper.DownloadAsBytesByName("QQQ"); 
   //Image img = ImageHelper.BytesToImage(Downdata); 
   //string path = Path.GetFullPath(@"../../DownLoadImg/") + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".jpg"; 
   ////使用path获取当前应用程序集的执行目录的上级的上级目录 
   //img.Save(path, System.Drawing.Imaging.ImageFormat.Jpeg); 
 
   #endregion 
 
   #region 查找图片 
   GridFSFileInfo gridFsFileInfo = helper.FindFiles("man"); 
   Console.WriteLine(gridFsFileInfo.Id); 
   #endregion 
 
   #region 删除图片 
   //helper.DroppGridFSBucket(); 
   #endregion 
 
   Console.ReadKey(); 
  } 
 } 
</div>

GridFSHelper.cs的代码如下:

using System; 
using System.Collections.Generic; 
using System.Configuration; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using MongoDB.Bson; 
using MongoDB.Driver; 
using MongoDB.Driver.GridFS; 
 
namespace MongoDemo 
{ 
 public class GridFSHelper 
 { 
  private readonly IMongoClient client; 
  private readonly IMongoDatabase database; 
  private readonly IMongoCollection<BsonDocument> collection; 
  private readonly GridFSBucket bucket; 
  private GridFSFileInfo fileInfo; 
  private ObjectId oid; 
 
  public GridFSHelper() 
   : this( 
    ConfigurationManager.AppSettings["mongoQueueUrl"], ConfigurationManager.AppSettings["mongoQueueDb"], 
    ConfigurationManager.AppSettings["mongoQueueCollection"]) 
  { 
  } 
 
  public GridFSHelper(string url, string db, string collectionName) 
  { 
   if (url == null) 
   { 
    throw new ArgumentNullException("url"); 
   } 
   else 
   { 
    client = new MongoClient(url); 
   } 
 
   if (db == null) 
   { 
    throw new ArgumentNullException("db"); 
   } 
   else 
   { 
    database = client.GetDatabase(db); 
   } 
 
   if (collectionName == null) 
   { 
    throw new ArgumentNullException("collectionName"); 
   } 
   else 
   { 
    collection = database.GetCollection<BsonDocument>(collectionName); 
   } 
 
   //this.collection = new MongoClient(url).GetDatabase(db).GetCollection<BsonDocument>(collectionName); 
 
   GridFSBucketOptions gfbOptions = new GridFSBucketOptions() 
   { 
    BucketName = "bird", 
    ChunkSizeBytes = 1*1024*1024, 
    ReadConcern = null, 
    ReadPreference = null, 
    WriteConcern = null 
   }; 
   var bucket = new GridFSBucket(database, new GridFSBucketOptions 
   { 
    BucketName = "videos", 
    ChunkSizeBytes = 1048576, // 1MB 
    WriteConcern = WriteConcern.WMajority, 
    ReadPreference = ReadPreference.Secondary 
   }); 
   this.bucket = new GridFSBucket(database, null); 
  } 
 
  public GridFSHelper(IMongoCollection<BsonDocument> collection) 
  { 
   if (collection == null) 
   { 
    throw new ArgumentNullException("collection"); 
   } 
   this.collection = collection; 
   this.bucket = new GridFSBucket(collection.Database); 
  } 
 
 
  public ObjectId UploadGridFSFromBytes(string filename, Byte[] source) 
  { 
   oid = bucket.UploadFromBytes(filename, source); 
   return oid; 
  } 
 
  public ObjectId UploadGridFSFromStream(string filename,Stream source) 
  { 
   using (source) 
   { 
    oid = bucket.UploadFromStream(filename, source); 
    return oid; 
   } 
  } 
 
  public Byte[] DownloadAsByteArray(ObjectId id) 
  { 
   Byte[] bytes = bucket.DownloadAsBytes(id); 
   return bytes; 
  } 
 
  public Stream DownloadToStream(ObjectId id) 
  { 
   Stream destination = new MemoryStream(); 
   bucket.DownloadToStream(id, destination); 
   return destination; 
  } 
 
  public Byte[] DownloadAsBytesByName(string filename) 
  { 
   Byte[] bytes = bucket.DownloadAsBytesByName(filename); 
   return bytes; 
  } 
 
  public Stream DownloadToStreamByName(string filename) 
  { 
   Stream destination = new MemoryStream(); 
   bucket.DownloadToStreamByName(filename, destination); 
   return destination; 
  } 
 
  public GridFSFileInfo FindFiles(string filename) 
  { 
   var filter = Builders<GridFSFileInfo>.Filter.And( 
   Builders<GridFSFileInfo>.Filter.Eq(x => x.Filename, "man"), 
   Builders<GridFSFileInfo>.Filter.Gte(x => x.UploadDateTime, new DateTime(2015, 1, 1, 0, 0, 0, DateTimeKind.Utc)), 
   Builders<GridFSFileInfo>.Filter.Lt(x => x.UploadDateTime, new DateTime(2017, 2, 1, 0, 0, 0, DateTimeKind.Utc))); 
   var sort = Builders<GridFSFileInfo>.Sort.Descending(x => x.UploadDateTime); 
   var options = new GridFSFindOptions 
   { 
    Limit = 1, 
    Sort = sort 
   }; 
   using (var cursor = bucket.Find(filter, options)) 
   { 
     fileInfo = cursor.ToList().FirstOrDefault(); 
   } 
   return fileInfo; 
  } 
 
 
  public void DeleteAndRename(ObjectId id) 
  { 
   bucket.Delete(id); 
  } 
 
  //The “fs.files” collection will be dropped first, followed by the “fs.chunks” collection. This is the fastest way to delete all files stored in a GridFS bucket at once. 
  public void DroppGridFSBucket() 
  { 
   bucket.Drop(); 
  } 
 
  public void RenameAsingleFile(ObjectId id,string newFilename) 
  { 
   bucket.Rename(id, newFilename); 
  } 
 
  public void RenameAllRevisionsOfAfile(string oldFilename,string newFilename) 
  { 
   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); 
   } 
  } 
 
 } 
} 
</div>

ImageHelper.cs的代码如下:

using System; 
using System.Collections.Generic; 
using System.Drawing; 
using System.Drawing.Imaging; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace MongoDemo 
{ 
 public static class ImageHelper 
 { 
  /// <summary> 
  /// //将Image转换成流数据,并保存为byte[]  
  /// </summary> 
  /// <param name="image"></param> 
  /// <returns></returns> 
  public static byte[] ImageToBytes(Image image) 
  { 
   ImageFormat format = image.RawFormat; 
   using (MemoryStream ms = new MemoryStream()) 
   { 
    if (format.Equals(ImageFormat.Jpeg)) 
    { 
     image.Save(ms, ImageFormat.Jpeg); 
    } 
    else if (format.Equals(ImageFormat.Png)) 
    { 
     image.Save(ms, ImageFormat.Png); 
    } 
    else if (format.Equals(ImageFormat.Bmp)) 
    { 
     imag
  


 
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • MongoDB.NET 2.2.4驱动版本对Mongodb3.3数据库中GridFS增删改查
  • PHP MongoDB GridFS 存储文件的方法详解
  • MongoDB系列教程(八):GridFS存储详解
  • Mongodb批量删除gridfs文件实例

相关文章

  • 2017-05-11mongodb replica set 配置高性能多服务器详解
  • 2017-05-11CentOS 6.5系统中使用yum安装MongoDB 2.6 教程
  • 2017-05-11MongoDB简介 MongoDB五大特色
  • 2017-05-11MongoDB系列教程(六):java操作mongodb实例
  • 2017-05-11MongoDB的Master-Slave主从模式配置及主从复制要点解析
  • 2017-05-11mongodb数据库的6个安全设置命令
  • 2017-05-11使用zabbix监控mongodb的方法
  • 2017-05-11在Mac OS上安装使用MongoDB的教程
  • 2017-05-11MongoDB db.serverStatus()输出内容中文注释
  • 2017-05-11MongoDB Remove函数的3个常见用法

文章分类

  • MsSql
  • Mysql
  • oracle
  • MariaDB
  • DB2
  • SQLite
  • PostgreSQL
  • MongoDB
  • Redis
  • Access
  • 数据库其它
  • sybase
  • HBase

最近更新的内容

    • mongoDB 实现主从读写分离实现的实例代码
    • 浅析Mongodb性能优化的相关问题
    • Mongodb中MapReduce实现数据聚合方法详解
    • MongoDB系列教程(六):java操作mongodb实例
    • Windows系统下安装MongoDB与Robomongo环境详解
    • 详解MongoDB中创建集合与删除集合的操作方法
    • MongoDB中MapReduce编程模型使用实例
    • Mongodb 数据类型及Mongoose常用CURD
    • mongodb数据库游标的使用浅析
    • MongoDB入门教程之Windows下的MongoDB数据库安装图解

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有