MongoDBѧϰ±Ê¼Ç(Îå) MongoDBÎļþ´æÈ¡²Ù×÷
¡¡¡¡ÓÉÓÚMongoDBµÄÎĵµ½á¹¹ÎªBJSON¸ñʽ£¨BJSONÈ«³Æ£ºBinary JSON£©£¬¶øBJSON¸ñʽ±¾Éí¾ÍÖ§³Ö±£´æ¶þ½øÖƸñʽµÄÊý¾Ý£¬Òò´Ë¿ÉÒÔ°ÑÎļþµÄ¶þ½øÖƸñʽµÄÊý¾ÝÖ±½Ó±£´æµ½MongoDBµÄÎĵµ½á¹¹ÖС£µ«ÊÇÓÉÓÚÒ»¸ö BJSONµÄ×î´ó³¤¶È²»Äܳ¬¹ý4M£¬ËùÒÔÏÞÖÆÁ˵¥¸öÎĵµÖÐÄÜ´æÈëµÄ×î´óÎļþ²»Äܳ¬¹ý4M¡£ÎªÁËÌṩ¶Ô´óÈÝÁ¿Îļþ´æÈ¡µÄÖ§³Ö£¬samusÇý¶¯ÌṩÁË ¡°GridFS¡±·½Ê½À´Ö§³Ö£¬¡°GridFS¡±·½Ê½Îļþ²Ù×÷ÐèÒªÒýÈëеijÌÐò¼¯¡°MongoDB.GridFS.dll¡±¡£ÏÂÃæÎÒÃÇ·Ö±ðÓÃÁ½ÖÖ·½Ê½À´Êµ ÏÖ¡£
Ò»¡¢ÔÚÎĵµ¶ÔÏóÖдæÈ¡Îļþ
¡¡¡¡µ±Îļþ´óС½ÏСµÄʱºò£¬Ö±½Ó´æÈëÎĵµ¶ÔÏóʵÏÖÆðÀ´¸ü¼ò½à¡£±ÈÈç´óÁ¿Í¼Æ¬ÎļþµÄ´æÈ¡µÈ£¬Ò»°ãͼƬÎļþ¶¼²»»á³¬¹ý4M¡£ÎÒÃÇÏÈʵÏÖÒ»¸öÉÏ´«Í¼Æ¬´æÈëÊý¾Ý¿â£¬ÔÙÈ¡³öÀ´Ð´»ØÒ³ÃæµÄÀý×Ó£º
¡¡¡¡ 1. °ÑͼƬ´æµ½BJSONÖÐ
/// <summary>
/// °ÑͼƬ´æµ½BJSONÖÐ
/// </summary>
public void SaveImgBJSON(byte[] byteImg)
{
Document doc = new Document();
doc["ID"] = 1;
doc["Img"] = byteImg;
mongoCollection.Save(doc);
}
2. »ñÈ¡BJSON·½Ê½´æ´¢µÄͼƬ×Ö½ÚÊý¾Ý
/// <summary>
/// »ñÈ¡BJSON·½Ê½´æ´¢µÄͼƬ×Ö½ÚÊý¾Ý
/// </summary>
public byte[] GetImgBJSON()
{
Document doc= mongoCollection.FindOne(new Document { { "ID", 1 } });
return doc["Img"] as Binary;
}
ÉÏÃæÁ½¶Î´úÂëÊÇÔÚ¶ÔMongoDBÏà¹Ø²Ù×÷½øÐÐBLL·â×°ÀàÖÐÌí¼ÓµÄÁ½¸ö·½·¨£¬·â×°·½Ê½²é¿´ÉϽÚÄÚÈÝ¡£ÏÂÃæ¿´¿´ÔÚwebformÖÐÈçºÎµ÷Óãº
¡¡¡¡ÔÚ½çÃæÍϳöÒ»¸öFileUpload¿Ø¼þºÍÒ»¸öButton¿Ø¼þ£¬Ò³ÃæcsÀà¼ÓÈçÏ·½·¨£º
protected void Button1_Click(object sender, EventArgs e) { ImgBLL imgBll = new ImgBLL(); imgBll.DeleteAll(); imgBll.SaveImgBJSON(FileUpload1.FileBytes); Response.BinaryWrite(imgBll.GetImgBJSON()); }
¶þ¡¢ÓÃGridFS·½Ê½´æÈ¡Îļþ
¡¡¡¡ÔÚʵÏÖGridFS·½Ê½Ç°ÎÒÏȽ²½²ËüµÄÔÀí£¬ÎªÊ²Ã´¿ÉÒÔ´æ´óÎļþ¡£Çý¶¯Ê×ÏÈ»áÔÚµ±Ç°Êý¾Ý¿â´´½¨Á½¸ö¼¯ºÏ£º"fs.files" ºÍ"fs.chunks"¼¯ºÏ£¬Ç°Õ߼ǼÁËÎļþÃû£¬Îļþ´´½¨Ê±¼ä£¬ÎļþÀàÐ͵Ȼù±¾ÐÅÏ¢£»ºóÕß·Ö¿é´æ´¢ÁËÎļþµÄ¶þ½øÖÆÊý¾Ý£¨²¢Ö§³Ö¼ÓÃÜÕâЩ¶þ½øÖÆÊý¾Ý£©¡£·Ö ¿éµÄÒâ˼ÊÇ°ÑÎļþ°´ÕÕÖ¸¶¨´óС·Ö¸î£¬È»ºó´æÈë¶à¸öÎĵµÖС£"fs.files"Ôõô֪µÀËü¶ÔÓ¦µÄÎļþ¶þ½øÖÆÊý¾ÝÔÚÄÄЩ¿éÄØ£¿ÄÇÊÇÒòΪ ÔÚ"fs.chunks"ÖÐÓиö"files_id"¼ü£¬Ëü¶ÔÓ¦"fs.files"µÄ"_id"¡£"fs.chunks"»¹ÓÐÒ»¸ö¼ü(int ÐÍ)"n"£¬Ëü±íÃ÷ÕâЩ¿éµÄÏȺó˳Ðò¡£ÕâÁ½¸ö¼¯ºÏÃûÖеÄ"fs"Ò²ÊÇ¿ÉÒÔͨ¹ý²ÎÊý×Ô¶¨ÒåµÄ¡£
¡¡¡¡Èç¹ûÄãÖ»ÊÇÏëÖªµÀÔõôÓ㬿ÉÒÔºöÂÔÉÏÃæÕâ¶Î»°£¬ÏÂÃ潫Ó÷¨£º
¡¡¡¡ 1. GridFS·½Ê½µÄÎļþн¨£¬¶ÁÈ¡£¬É¾³ý
private string GridFsSave(byte[] byteFile)
{
string filename = Guid.NewGuid().ToString();
//ÕâÀïGridFile¹¹Ô캯ÊýÓиöÖØÔØ£¬bucket²ÎÊý¾ÍÊÇÓÃÀ´Ìæ»»ÄǸö´´½¨¼¯ºÏÃûÖÐĬÈϵÄ"fs"µÄ¡£
GridFile gridFile = new GridFile(mongoDatabase);
using (GridFileStream gridFileStream = gridFile.Create(filename))
{
gridFileStream.Write(byteFile, 0, byteFile.Length);
}
return filename;
}
private byte[] GridFsRead(string filename)
{
GridFile gridFile = new GridFile(mongoDatabase);
GridFileStream gridFileStream = gridFile.OpenRead(filename);
byte[] bytes = new byte[gridFileStream.Length];
gridFileStream.Read(bytes, 0, bytes.Length);
return bytes;
}
private void GridFsDelete(string filename)
{
GridFile gridFile = new GridFile(mongoDatabase);
gridFile.Delete(new Document("filename", filename));
}
2. Ôٴηâ×°GridFS²Ù×÷£¬ÐÂÎĵµÖ»´æ´¢ÎļþÃû³Æ£¬Ï൱ÓÚÖ»ÊÇÒ»¸ö¼ü£¬ÐÂÎĵµ»¹¿ÉÒÔÓгý¡°ÎļþÃû¡±Ö®ÍâÆäËûµÄ¼ü¡£
/// <summary>
/// °ÑͼƬ´æµ½GridFSÖÐ
/// </summary>
public void SaveImgGridFS(byte[] byteImg)
{
string filename = GridFsSave(byteImg);
Document doc = new Document();
doc["ID"] = 1;
doc["filename"] = filename;
mongoCollection.Save(doc);
}
/// <summary>
/// »ñÈ¡GridFS·½Ê½´æ´¢µÄͼƬ
/// </summary>
public byte[] GetImgGridFS()
{
Document doc = mongoCollection.FindOne(new Document { { "ID", 1 } });
string filename = doc["filename"].ToString();
return GridFsRead(filename);
}
Èý¡¢Ð¡½á
¡¡¡¡Îļþ´æÈ¡Ó¦¸Ã²»ÊǺÜÄÑ£¬ÖµµÃ×¢ÒâµÄµØ·½ÊÇ£ºÓõÚÒ»ÖÖ·½Ê½´ÓÎĵµÖжÁ³ö¶þ½øÖÆÊý¾Ýʱ£¬Ò»¶¨Òª½«ÀàÐÍת»»Îª¡°Binary¡±ÀàÐÍ£»»¹ÓÐϵͳ×Ô´øµÄ¼ü¡°_id¡±£¬ËüÒ²²»ÊÇstringÀàÐÍ£¬ÊÇ¡°Oid¡±ÀàÐ͵ġ£
ÍƼöÐÅÏ¢
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- php³£ÓÃÕýÔò±í´ïʽ
- phpÐÔÄܼà²âÄ£¿éXHProf
- ÈÃCI¿ò¼ÜÖ§³Öservice²ã
- ʹÓÃPHPÉú³É´øLOGOµÄ¸öÐÔ»¯¶þάÂëͼÏñ
- ¹ØÓÚCodeIgniterÄã¿ÉÄܲ»ÖªµÀµÄ5¸ö֪ʶµã
- Memcache ºÁÃ뼶³¬Ê±¼°ÆäËû³£¼ûÎÊÌâ»ã×Ü
- [PHP±Ê¼Ç]PHPQueryÒ»¸ö´¦ÀíDOMµÄÀûÆ÷
ÈÈÃÅÐÅÏ¢
- nohup: redirecting stderr to stdou....
- ʹÓÃlog_formatΪNginx·þÎñÆ÷ÉèÖøüÏêϸµÄÈÕÖ¾¸ñʽ
- jquery easyUI--dataGrid-Json
- [Ô´´]·ÂGoogle Reader¡¢ÐÂÀË΢²©¡¢ÌÚѶ΢²©µ....
- ÀûÓÃKeepalived+mysql¹¹½¨¸ß¿ÉÓÃMySQLË«Ö÷×Ô¶....
- Nginx+keepalivedʵÏÖ¸ºÔؾùºâºÍË«»úÈȱ¸¸ß¿ÉÓÃ
- jqueryʵÏÖÒ³Ãæ¼ÓÔؽø¶ÈÌõ
- Rolling cURL: PHP²¢·¢×î¼Ñʵ¼ù
- codeigniter ·ÓÉÖÕ¼«ÓÅ»¯(url rewrite)
- linuxÏÂÉèÖÃsshÎÞÃÜÂëµÇ¼
×î½ü¸üÐÂ
- PHP»ñÈ¡Óû§µÄÕæʵIP£¬²¢ÅжÏÊÇ·ñÄÚÍøIP
- PHP ´íÎóÈÕÖ¾ error_log
- ÀûÓÃbigpipe»úÖÆʵÏÖÒ³ÃæÄ£¿éµÄÒì²½äÖȾ chunked¼¼Êõ
- php¿ØÖÆÎļþÏÂÔØËÙ¶È
- js + php ¶ÁÈ¡¡¢²¥·ÅÊÓƵÁ÷ ¼æÈÝfirefox£¬c....
- ¡¾ÊÓƵ²¥·Å¡¿JplayerÊÓƵ²¥·ÅÆ÷µÄʹÓÃ
- UNICODE Óë UTF-8 µÄ¹Øϵ
- memcacheÄÚ´æÔÀí
- Memcache¼¼Êõ·ÖÏí£º½éÉÜ¡¢Ê¹Óᢴ洢¡¢Ëã·¨¡¢ÓÅ»¯....
- phpʹÓÃmb_detect_encoding¼ì²â×Ö·û´®±àÂë
ÆÀÂÛ