mongodb 学习笔记四 GridFS Example
来源:互联网 发布:udp514端口会丢包吗 编辑:程序博客网 时间:2024/05/22 09:06
直接贴英文,翻译不好反而影响读者,英文如下:
GridFS Example
This example shows how to use gridfs to store large binaryobjects (e.g. files) in MongoDB.
See also
The API docs for gridfs.
See also
This blog postfor some motivation behind this API.
Setup
We start by creating a GridFS instance to use:
>>> from pymongo import Connection>>> import gridfs>>>>>> db = Connection().gridfs_example>>> fs = gridfs.GridFS(db)
Every GridFS instance is created with and willoperate on a specificDatabase instance.
Saving and Retrieving Data
The simplest way to work with gridfs is to use its key/valueinterface (theput() andget() methods). To write data to GridFS, useput():
>>> a = fs.put("hello world")
put() creates a new file in GridFS, and returnsthe value of the file document’s "_id" key. Given that"_id"we can use get() to get back the contents of thefile:
>>> fs.get(a).read()'hello world'
get() returns a file-like object, so we get thefile’s contents by calling read().
In addition to putting a str as a GridFS file, we can alsoput any file-like object (an object with aread()method). GridFS will handle reading the file in chunk-sized segmentsautomatically. We can also add additional attributes to the file askeyword arguments:
>>> b = fs.put(fs.get(a), filename="foo", bar="baz")>>> out = fs.get(b)>>> out.read()'hello world'>>> out.filenameu'foo'>>> out.baru'baz'>>> out.upload_datedatetime.datetime(...)
The attributes we set in put() are stored in thefile document, and retrievable after callingget(). Some attributes (like "filename") arespecial and are defined in the GridFS specification - see thatdocument for more details.
例子源码如下:
from pymongo import Connectionimport gridfsdb = Connection().gridfs_examplefs = gridfs.GridFS(db)a = fs.put("hello world")print acontent = fs.get(a).read()print contentb = fs.put(fs.get(a), filename="foo", bar="baz")out = fs.get(b)print bprint out.filenameprint out.bar
- mongodb 学习笔记四 GridFS Example
- MongoDB学习笔记--GridFS
- MongoDB学习笔记GridFS
- MongoDB的学习笔记(四)--MongoDB中的GridFS
- MongoDB学习笔记<四>
- mongoDB学习笔记四
- MongoDB学习之旅十一:MongoDB GridFS
- MongoDB 数据库操作(四)-GridFS
- ToLua学习笔记(四) Example 04 AccessingLuaVariables
- MongoDB---GridFS
- MongoDB GridFS
- MongoDB GridFS
- mongodb GridFS
- MongoDB GridFS
- mongodb学习(固定集合&GridFs文件系统)
- MongoDB 学习笔记(五):固定集合、GridFS文件系统与服务器端脚本
- Mongodb学习笔记四、MongodbShell
- MongoDB 学习笔记四 C#调用MongoDB
- C++类对应的内存结构
- 数据流(一)
- [推荐]查看Json输出的*最方便*的方法(转http://www.cnblogs.com/1-2-3/archive/2009/11/24/json-viewer-extjs-mvc-fiddle)
- 数据流(二)
- 装饰模式
- mongodb 学习笔记四 GridFS Example
- 对象的序列化
- 闲聊linux中的input设备之前言
- C++中类的内存结构解析
- 序列流
- 闲聊linux中的input设备(1)Linux中的设备大家族
- java删除字符串中的指定字符
- js限制只能输入日期或时间的输入框,并加验证
- 用flatworm解析和生成Flat Files开发指南