MongoDB GridFS命令列工具mongofiles使用舉例

chenfeng發表於2016-05-29
我們先看下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 ( <set
                                        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




</set

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/15498/viewspace-2108870/,如需轉載,請註明出處,否則將追究法律責任。

相關文章