Home > Database > Mysql Tutorial > 用MongoDB基于GridFS存储文件

用MongoDB基于GridFS存储文件

WBOY
Release: 2016-06-07 17:36:17
Original
976 people have browsed it

在Mongodb中以GridFSB方式存放文件有两种方式:1、命令行方式mongofiles 2、客户端驱动编程(以Java为例)

在Mongodb中以GridFSB方式存放文件有两种方式:1、命令行方式mongofiles 2、客户端驱动编程(以Java为例)

相关阅读:

MongoDB集群配置(sharding with replica set)

MongoDB运行时添加分片复制集

1、命令行方式mongofiles

在bin目录下有一个命令mongofiles.exe,即可完成命令行下向Mongodb数据库中插入文件数据。这里以前述所搭的4个分片,每分片3个复制集,3个ConfigSever ,,1个路由节点的Windows的模拟集群为例。

D:/mongodb-win32-i386-1.8.0/bin>mongofiles.exe -host 127.0.0.1:50000 -d wlb put mongodump.exe
connected to: 127.0.0.1:50000
added file: { _id: ObjectId('4d91dbc1fb02c495a0a479d7'), filename: "mongodump.exe", chunkSize: 262144, uploadDate: new Date(1301404609850), md5: "647dcae88077e6
fa2bb3be6b0cdebb07", length: 3271680 }
done!

向数据库wlb中插入一个文件,即Mongodb/bin/mongodump.exe,所使用的Mongodb集群的路由节点为127.0.0.1,端口为50000,其中put为命令,表示向Mongodb中上传文件,也存在get、delete两个其它命令,分别表示取得文件和删除文件。

以如下命令启动mongo,进入wlb数据库,执行 db.fs.files.find()即可看到GridFS中的文件列表.

D:/mongodb-win32-i386-1.8.0/cmd>cd d:/mongodb-win32-i386-1.8.0/bin
D:/mongodb-win32-i386-1.8.0/bin>call mongo.exe 127.0.0.1:50000/admin
MongoDB shell version: 1.8.0
connecting to: 127.0.0.1:50000/admin
> use wlb
switched to db wlb
> db.fs.files.find()
{ "_id" : ObjectId("4d91dbc1fb02c495a0a479d7"), "filename" : "mongodump.exe", "chunkSize" : 262144, "uploadDate" : ISODate("2011-03-29T13:16:49.850Z"), "md5" :
"647dcae88077e6fa2bb3be6b0cdebb07", "length" : 3271680 }

2、以Java API来存取文件

这里以mongo-2.5.jar为例,代码如下:

linux

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template