最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

MongoDB.NET 2.2.4驅(qū)動(dòng)版本對(duì)Mongodb3.3數(shù)據(jù)庫(kù)中GridFS增刪改查

 更新時(shí)間:2020年04月10日 11:31:01   作者:天風(fēng)隼  
這篇文章主要為大家詳細(xì)介紹了使用MongoDB.NET 2.2.4驅(qū)動(dòng)版本對(duì)Mongodb3.3數(shù)據(jù)庫(kù)中GridFS增刪改查,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了針對(duì)Mongodb3.3數(shù)據(jù)庫(kù)中GridFS增刪改查,供大家參考,具體內(nèi)容如下

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獲取當(dāng)前應(yīng)用程序集的執(zhí)行目錄的上級(jí)的上級(jí)目錄 
  //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(); 
 } 
 } 

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); 
  } 
 } 
 
 } 
} 

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轉(zhuǎn)換成流數(shù)據(jù),并保存為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)) 
  { 
   image.Save(ms, ImageFormat.Bmp); 
  } 
  else if (format.Equals(ImageFormat.Gif)) 
  { 
   image.Save(ms, ImageFormat.Gif); 
  } 
  else if (format.Equals(ImageFormat.Icon)) 
  { 
   image.Save(ms, ImageFormat.Icon); 
  } 
  byte[] buffer = new byte[ms.Length]; 
  //Image.Save()會(huì)改變MemoryStream的Position,需要重新Seek到Begin 
  ms.Seek(0, SeekOrigin.Begin); 
  ms.Read(buffer, 0, buffer.Length); 
  return buffer; 
  } 
 } 
 
 
 public static Stream ImageToStream(Image image) 
 { 
  ImageFormat format = image.RawFormat; 
  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)) 
  { 
  image.Save(ms, ImageFormat.Bmp); 
  } 
  else if (format.Equals(ImageFormat.Gif)) 
  { 
  image.Save(ms, ImageFormat.Gif); 
  } 
  else if (format.Equals(ImageFormat.Icon)) 
  { 
  image.Save(ms, ImageFormat.Icon); 
  } 
  return ms; 
 } 
 
 //參數(shù)是圖片的路徑 
 public static byte[] GetPictureData(string imagePath) 
 { 
  FileStream fs = new FileStream(imagePath, FileMode.Open); 
  byte[] byteData = new byte[fs.Length]; 
  fs.Read(byteData, 0, byteData.Length); 
  fs.Close(); 
  return byteData; 
 } 
 
 
 
 /// <summary> 
 /// Convert Byte[] to Image 
 /// </summary> 
 /// <param name="buffer"></param> 
 /// <returns></returns> 
 public static Image BytesToImage(byte[] buffer) 
 { 
  MemoryStream ms = new MemoryStream(buffer); 
  Image image = System.Drawing.Image.FromStream(ms); 
  return image; 
 } 
 
 /// <summary> 
 /// Convert Byte[] to a picture and Store it in file 
 /// </summary> 
 /// <param name="fileName"></param> 
 /// <param name="buffer"></param> 
 /// <returns></returns> 
 public static string CreateImageFromBytes(string fileName, byte[] buffer) 
 { 
  string file = fileName; 
  Image image = BytesToImage(buffer); 
  ImageFormat format = image.RawFormat; 
  if (format.Equals(ImageFormat.Jpeg)) 
  { 
  file += ".jpg"; 
  } 
  else if (format.Equals(ImageFormat.Png)) 
  { 
  file += ".png"; 
  } 
  else if (format.Equals(ImageFormat.Bmp)) 
  { 
  file += ".bmp"; 
  } 
  else if (format.Equals(ImageFormat.Gif)) 
  { 
  file += ".gif"; 
  } 
  else if (format.Equals(ImageFormat.Icon)) 
  { 
  file += ".icon"; 
  } 
  System.IO.FileInfo info = new System.IO.FileInfo(Path.GetFullPath(@"DownLoadImg\")); //在當(dāng)前程序集目錄中添加指定目錄DownLoadImg 
  System.IO.Directory.CreateDirectory(info.FullName); 
  File.WriteAllBytes(info+file, buffer); 
  return file; 
 } 
 } 
} 

LogHelper.cs代碼如下:

/// <summary> 
 /// 手動(dòng)記錄錯(cuò)誤日志,不用Log4Net組件 
 /// </summary> 
 public class LogHelper 
 { 
 /// <summary> 
 /// 將日志寫入指定的文件 
 /// </summary> 
 /// <param name="Path">文件路徑,如果沒有該文件,剛創(chuàng)建</param> 
 /// <param name="content">日志內(nèi)容</param> 
 public static void WriteFile(string content) 
 { 
  string Path = AppDomain.CurrentDomain.BaseDirectory + "Log"; 
  if (!Directory.Exists(Path)) 
  { 
  //若文件目錄不存在 則創(chuàng)建 
  Directory.CreateDirectory(Path); 
  } 
  Path += "\\" + DateTime.Now.ToString("yyMMdd") + ".log"; 
  if (!File.Exists(Path)) 
  { 
  File.Create(Path).Close(); 
  } 
  StreamWriter writer = new StreamWriter(Path, true, Encoding.GetEncoding("gb2312")); 
  writer.WriteLine("時(shí)間:" + DateTime.Now.ToString()); 
  writer.WriteLine("日志信息:" + content); 
  writer.WriteLine("-----------------------------------------------------------"); 
  writer.Close(); 
  writer.Dispose(); 
 } 
 
 /// <summary> 
 /// 將日志寫入指定的文件 
 /// </summary> 
 /// <param name="Path">文件路徑,如果沒有該文件,剛創(chuàng)建</param> 
 /// <param name="content">日志內(nèi)容</param> 
 public static void WriteFile(int content) 
 { 
  string Path = AppDomain.CurrentDomain.BaseDirectory + "Log"; 
  if (!Directory.Exists(Path)) 
  { 
  //若文件目錄不存在 則創(chuàng)建 
  Directory.CreateDirectory(Path); 
  } 
  Path += "\\" + DateTime.Now.ToString("yyMMdd") + ".log"; 
  if (!File.Exists(Path)) 
  { 
  File.Create(Path).Close(); 
  } 
  StreamWriter writer = new StreamWriter(Path, true, Encoding.GetEncoding("gb2312")); 
  writer.WriteLine("時(shí)間:" + DateTime.Now.ToString()); 
  writer.WriteLine("日志信息:" + content); 
  writer.WriteLine("-----------------------------------------------------------"); 
  writer.Close(); 
  writer.Dispose(); 
 } 
 
 
 /// <summary> 
 /// 將日志寫入指定的文件 
 /// </summary> 
 /// <param name="erroMsg">錯(cuò)誤詳細(xì)信息</param> 
 /// <param name="source">源位置</param> 
 /// <param name="fileName">文件名</param> 
 public static void WriteFile(string erroMsg, string source, string stackTrace, string fileName) 
 { 
  string Path = AppDomain.CurrentDomain.BaseDirectory + "Log"; 
  if (!Directory.Exists(Path)) 
  { 
  //若文件目錄不存在 則創(chuàng)建 
  Directory.CreateDirectory(Path); 
  } 
  Path += "\\" + DateTime.Now.ToString("yyMMdd") + ".log"; 
  if (!File.Exists(Path)) 
  { 
  File.Create(Path).Close(); 
  } 
  StreamWriter writer = new StreamWriter(Path, true, Encoding.GetEncoding("gb2312")); 
  writer.WriteLine("時(shí)間:" + DateTime.Now.ToString()); 
  writer.WriteLine("文件:" + fileName); 
  writer.WriteLine("源:" + source); 
  writer.WriteLine("錯(cuò)誤信息:" + erroMsg); 
  writer.WriteLine("-----------------------------------------------------------"); 
  writer.Close(); 
  writer.Dispose(); 
 } 
 } 

結(jié)果如下:

Mongodb數(shù)據(jù):

查找圖片:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • MongoDB常用操作命令大全

    MongoDB常用操作命令大全

    成功啟動(dòng)MongoDB后,再打開一個(gè)命令行窗口輸入mongo,就可以進(jìn)行數(shù)據(jù)庫(kù)的一些操作。輸入help可以看到基本操作命令,只是MongoDB沒有創(chuàng)建數(shù)據(jù)庫(kù)的命令,但有類似的命令
    2014-03-03
  • ?PostgreSQL?與MongoDB使用對(duì)比分析

    ?PostgreSQL?與MongoDB使用對(duì)比分析

    這篇文章主要介紹了為什么?PostgreSQL?能代替?MongoDB?,需要的朋友可以參考下
    2023-12-12
  • mongodb 3.4下遠(yuǎn)程連接認(rèn)證失敗的解決方法

    mongodb 3.4下遠(yuǎn)程連接認(rèn)證失敗的解決方法

    這篇文章主要給大家介紹了在mongodb 3.4下遠(yuǎn)程連接認(rèn)證失敗的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編一起來學(xué)習(xí)學(xué)習(xí)吧。
    2017-06-06
  • MongoDB快速入門筆記(六)之MongoDB的文檔修改操作

    MongoDB快速入門筆記(六)之MongoDB的文檔修改操作

    這篇文章主要介紹了MongoDB快速入門筆記(六)之MongoDB的文檔修改操作的相關(guān)資料,需要的朋友可以參考下
    2016-06-06
  • mongodb數(shù)據(jù)庫(kù)實(shí)驗(yàn)之增刪查改

    mongodb數(shù)據(jù)庫(kù)實(shí)驗(yàn)之增刪查改

    這篇文章主要介紹了mongodb數(shù)據(jù)庫(kù)實(shí)驗(yàn)之增刪查改的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • MongoDB數(shù)據(jù)去重與保存最新數(shù)據(jù)操作指南

    MongoDB數(shù)據(jù)去重與保存最新數(shù)據(jù)操作指南

    在 MongoDB 數(shù)據(jù)庫(kù)中,我們經(jīng)常需要進(jìn)行數(shù)據(jù)去重并保留最新的數(shù)據(jù),本文將介紹如何使用 MongoDB 聚合操作完成這一任務(wù),并將結(jié)果保存到新的集合或者覆蓋原有的集合,感興趣的小伙伴跟著小編一起來看看吧
    2024-01-01
  • MongoDB下根據(jù)數(shù)組大小進(jìn)行查詢的方法

    MongoDB下根據(jù)數(shù)組大小進(jìn)行查詢的方法

    這篇文章主要介紹了MongoDB下根據(jù)數(shù)組大小進(jìn)行查詢的方法,分別實(shí)現(xiàn)了指定大小的數(shù)組和某個(gè)范圍的數(shù)組,需要的朋友可以參考下
    2014-04-04
  • MongoDB聚合運(yùn)算符$toBool詳解

    MongoDB聚合運(yùn)算符$toBool詳解

    $toBool聚合運(yùn)算符將指定的值轉(zhuǎn)換為布爾類型boolean,這篇文章主要介紹了MongoDB聚合運(yùn)算符:$toBool的相關(guān)知識(shí),需要的朋友可以參考下
    2024-05-05
  • MongoDB數(shù)據(jù)庫(kù)基礎(chǔ)操作總結(jié)

    MongoDB數(shù)據(jù)庫(kù)基礎(chǔ)操作總結(jié)

    這篇文章主要介紹了MongoDB數(shù)據(jù)庫(kù)基礎(chǔ)操作,結(jié)合實(shí)例形式總結(jié)分析了MongoDB數(shù)據(jù)庫(kù)創(chuàng)建、刪除、集合、文檔等基本操作技巧,需要的朋友可以參考下
    2020-06-06
  • MongoDB添加secondary節(jié)點(diǎn)的2種方法詳解

    MongoDB添加secondary節(jié)點(diǎn)的2種方法詳解

    這篇文章主要給大家總結(jié)介紹了關(guān)于MongoDB添加secondary節(jié)點(diǎn)的2種方法,以及MongoDB secondary節(jié)點(diǎn)出現(xiàn)recovering狀態(tài)的解決方法,文中介紹的非常詳細(xì),需要的朋友可以參考下
    2018-10-10

最新評(píng)論

福泉市| 无极县| 女性| 肇东市| 西吉县| 颍上县| 鄄城县| 涟源市| 太仓市| 安溪县| 庄浪县| 应用必备| 建昌县| 子洲县| 玉门市| 麻城市| 盖州市| 新余市| 万荣县| 沂水县| 万全县| 宜都市| 盐源县| 双辽市| 沙洋县| 富宁县| 科技| 南漳县| 盐津县| 商南县| 瑞丽市| 三明市| 西充县| 普安县| 东乡| 呈贡县| 房产| 浙江省| 临江市| 彰化县| 二连浩特市|