MongoDB GridFS命令行工具mongofiles使用举例

我们先看下mongofiles的帮助:

C:\Users\duansf>mongofiles --help
Browse and modify a GridFS filesystem.


usage: mongofiles [options] command [gridfs filename]
command:
  one of (list|search|put|get)
  list - list all files.  'gridfs filename' is an optional prefix
         which listed filenames must begin with.
  search - search all files. 'gridfs filename' is a substring
           which listed filenames must contain.
  put - add a file with filename 'gridfs filename'
  get - get a file with filename 'gridfs filename'
  delete - delete all files with filename 'gridfs filename'
Options:
  --help                                produce help message
  -v [ --verbose ]                      be more verbose (include multiple times
                                        for more verbosity e.g. -vvvvv)
  --quiet                               silence all non error diagnostic
                                        messages
  --version                             print the program's version and exit
  -h [ --host ] arg                     mongo host to connect to (                                         name>/s1,s2 for sets)
  --port arg                            server port. Can also use --host
                                        hostname:port
  --ipv6                                enable IPv6 support (disabled by
                                        default)
  -u [ --username ] arg                 username
  -p [ --password ] arg                 password
  --authenticationDatabase arg          user source (defaults to dbname)
  --authenticationMechanism arg (=MONGODB-CR)
                                        authentication mechanism
  --gssapiServiceName arg (=mongodb)    Service name to use when authenticating
                                        using GSSAPI/Kerberos
  --gssapiHostName arg                  Remote host name to use for purpose of
                                        GSSAPI/Kerberos authentication
  --dbpath arg                          directly access mongod database files
                                        in the given path, instead of
                                        connecting to a mongod  server - needs
                                        to lock the data directory, so cannot
                                        be used if a mongod is currently
                                        accessing the same path
  --directoryperdb                      each db is in a separate directory
                                        (relevant only if dbpath specified)
  --journal                             enable journaling (relevant only if
                                        dbpath specified)
  -d [ --db ] arg                       database to use
  -c [ --collection ] arg               collection to use (some commands)
  -l [ --local ] arg                    local filename for put|get (default is
                                        to use the same name as 'gridfs
                                        filename')
  -t [ --type ] arg                     MIME type for put (default is to omit)
  -r [ --replace ]                      Remove other files with same name after
                                        PUT

举例说明用法:
1).列出数据库中的所有文件:
C:\Users\duansf>mongofiles list
connected to: 127.0.0.1


2).添加文件(需要指定路径):

C:\Users\duansf>mongofiles put C:\duansf\README.txt
connected to: 127.0.0.1
added file: { _id: ObjectId('574acae594a9b76720e6768b'), filename: "C:\duansf\RE
ADME.txt", chunkSize: 261120, uploadDate: new Date(1464519397095), md5: "5827acc
dd624438fd1a4753dd652de2b", length: 251 }
done!


3).查看数据库中的文件:
C:\Users\duansf>mongofiles list
connected to: 127.0.0.1
C:\duansf\README.txt    251   --代表文件长度

查看MongoDB中的数据:
C:\Users\duansf>mongo
MongoDB shell version: 2.6.6
connecting to: test
> db.fs.files.find()   --默认情况下,files集合在fs名称空间中创建
{ "_id" : ObjectId("574acae594a9b76720e6768b"), "filename" : "C:\\duansf\\README
.txt", "chunkSize" : 261120, "uploadDate" : ISODate("2016-05-29T10:56:37.095Z"),
 "md5" : "5827accdd624438fd1a4753dd652de2b", "length" : 251 }
> exit
bye

4).支持搜索命令:
C:\Users\duansf>mongofiles search README.txt
connected to: 127.0.0.1
C:\duansf\README.txt    251


5).删除:
C:\Users\duansf>mongofiles delete C:\duansf\README.txt
connected to: 127.0.0.1
done!


再次查看数据库中的文件,发现已删除:
C:\Users\duansf>mongofiles list
connected to: 127.0.0.1


C:\Users\duansf>mongo
MongoDB shell version: 2.6.6
connecting to: test
> db.fs.files.find()
>

当然也可以上传avi文件:
C:\Users\duansf>mongofiles put C:\Users\duansf\Desktop\mongo.avi
connected to: 127.0.0.1
added file: { _id: ObjectId('574bb4b1a33eea4b12f0127a'), filename: "C:\Users\dua
nsf\Desktop\mongo.avi", chunkSize: 261120, uploadDate: new Date(1464579257427),
md5: "fb1ddd09355a9764d9f87700bc48dc70", length: 40161178 }
done!

C:\Users\duansf>mongo

> db.fs.files.find().pretty()
{
        "_id" : ObjectId("574bb4b1a33eea4b12f0127a"),
        "filename" : "C:\\Users\\duansf\\Desktop\\mongo.avi",
        "chunkSize" : 261120,
        "uploadDate" : ISODate("2016-05-30T03:34:17.427Z"),
        "md5" : "fb1ddd09355a9764d9f87700bc48dc70",
        "length" : 40161178
}
>
C:\Users\duansf>mongofiles list
connected to: 127.0.0.1
C:\Users\duansf\Desktop\mongo.avi       40161178




来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/15498/viewspace-2108870/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/15498/viewspace-2108870/

你可能感兴趣的:(MongoDB GridFS命令行工具mongofiles使用举例)