chakey 发表于 2013-1-30 01:54:27

【6】MongoDB数据导入和导出

MongoDB数据导入和导出
 
元数据需要事先创建好。
mongoimport
这个命令可以导入单个JSON/CSV/TSV格式的文件。文件的每一行都要是指定格式的标准格式。
需要指定一个数据库(database)和一个collection(相当于关系数据库中的表)。
×××××××××××××
options:
  --help                  produce help message
  -v [ --verbose ]        be more verbose (include multiple times for more
                          verbosity e.g. -vvvvv)
  -h [ --host ] arg       mongo host to connect to ("left,right" for pairs)
  -d [ --db ] arg         database to use
  -c [ --collection ] arg collection to use (some commands)
  -u [ --username ] arg   username
  -p [ --password ] arg   password
  --dbpath arg            directly access mongod data files in the given path,
                          instead of connecting to a mongod instance - needs to
                          lock the data directory, so cannot be used if a
                          mongod is currently accessing the same path
  --directoryperdb        if dbpath specified, each db is in a separate
                          directory
  -f [ --fields ] arg     comma seperated list of field names e.g. -f name,age
  --fieldFile arg         file with fields names - 1 per line
  --ignoreBlanks          if given, empty fields in csv and tsv will be ignored
  --type arg              type of file to import.  default: json (json,csv,tsv)
  --file arg              file to import from; if not specified stdin is used
  --drop                  drop collection first
  --headerline            CSV,TSV only - use first line as headers
  ×××××××××××××
  MongoDB提供了比JSON丰富的多的数据格式,例如JSON中没有date数据类型,但是通过如下结构:
  {"somefield" : 123456, "created_at" : {"$date" : 1285679232000}}
  mongoimport 将create_at转换为Date类型数据了。
  注意:像这里的$date这样的实现确定的数据类型必须加双引号,否则无法正确解析。
 
  mongoexport
  这个工具可以将一个collection导出为JSON为CSV格式的文件。可以指定导出哪些数据项,也可以根据给
  定的条件导出数据。
  如果想导出CSV格式的文件,需要指定数据项的次序。
  ×××××××××××××
  options:
  --help                  produce help message
  -v [ --verbose ]        be more verbose (include multiple times for more
                          verbosity e.g. -vvvvv)
  -h [ --host ] arg       mongo host to connect to ("left,right" for pairs)
  -d [ --db ] arg         database to use
  -c [ --collection ] arg where 'arg' is the collection to use
  -u [ --username ] arg   username
  -p [ --password ] arg   password
  --dbpath arg            directly access mongod data files in the given path,
                          instead of connecting to a mongod instance - needs to
                          lock the data directory, so cannot be used if a
                          mongod is currently accessing the same path
  --directoryperdb        if dbpath specified, each db is in a separate
                          directory
  -q [ --query ] arg      query filter, as a JSON string
  -f [ --fields ] arg     comma seperated list of field names e.g. -f name,age
  --csv                   export to csv instead of json
  -o [ --out ] arg        output file; if not specified, stdout is used
 ×××××××××××××
 
 mongodump
 这个用于对数据库进行备份。
  ×××××××××××××
 options:
  --help                   produce help message
  -v [ --verbose ]         be more verbose (include multiple times for more
                           verbosity e.g. -vvvvv)
  -h [ --host ] arg        mongo host to connect to ("left,right" for pairs)
  -d [ --db ] arg          database to use
  -c [ --collection ] arg  collection to use (some commands)
  -u [ --username ] arg    username
  -p [ --password ] arg    password
  --dbpath arg             directly access mongod data files in the given path,
                           instead of connecting to a mongod instance - needs
                           to lock the data directory, so cannot be used if a
                           mongod is currently accessing the same path
  --directoryperdb         if dbpath specified, each db is in a separate
                           directory
  -o [ --out ] arg (=dump) output directory
  -q [ --query ] arg       json query
   ×××××××××××××
   例如:导出所有数据库中的所有collects,使用mongodump加上--host参数就可以了,在本地会产生一个dump命名的文件夹。
    ×××××××××××××
   $ ./mongodump --host prod.example.com
   connected to: prod.example.com
   all dbs
   DATABASE: log    to   dump/log
        log.errors to dump/log/errors.bson
                713 objects
        log.analytics to dump/log/analytics.bson
                234810 objects
    DATABASE: blog    to    dump/blog
        blog.posts to dump/log/blog.posts.bson
                59 objects
    DATABASE: admin    to    dump/admin
     ×××××××××××××
 
    例如:导出指定数据库中的一个指定的collection,导出结果是一个.bson文件。
    $ ./mongodump --db blog --collection posts
      connected to: 127.0.0.1
      DATABASE: blog        to     dump/blog
        blog.posts to dump/blog/posts.bson
                 59 objects
 
 
  在mongodb 1.7.0版本或更高的版本中,还可以将指定的collect通过标准输出流输出。
  这时需要指定 --out 参数
  $ ./mongodump --db blog --collection posts --out - > blogposts.bson
  一次只能导出一个collection到标准输出流文件中。
 
  mongorestore
  这个工具获取mongodump的输出结果然后重新存储。重新存储的过程会加上索引。
  ×××××××××××××
  usage: ./mongorestore
options:
  --help                  produce help message
  -v [ --verbose ]        be more verbose (include multiple times for more
                          verbosity e.g. -vvvvv)
  -h [ --host ] arg       mongo host to connect to ("left,right" for pairs)
  -d [ --db ] arg         database to use
  -c [ --collection ] arg collection to use (some commands)
  -u [ --username ] arg   username
  -p [ --password ] arg   password
  --dbpath arg            directly access mongod data files in the given path,
                          instead of connecting to a mongod instance - needs to
                          lock the data directory, so cannot be used if a
                          mongod is currently accessing the same path
  --directoryperdb        if dbpath specified, each db is in a separate
                          directory
  --drop                  drop each collection before import
  --objcheck              validate object before inserting
  --filter arg            filter to apply before inserting
  --drop                  drop each collection before import
  --indexesLast           wait to add indexes (faster if data isn't inserted in
                          index order)
  ×××××××××××××
 
  注意:如果不想创建索引的话,可以从数据库导出的文件夹(dump文件夹)中删除system.indexses.bson文件。
 
  bsondump
  这个是在1.6版本中新增加的。
  这个工具将一个bson文件转换为一个json/debug输出文件。
  ×××××××××××××××
  usage: ./bsondump
  options:
  --help                  produce help message
  --type arg (=json)      type of output: json,debug
  ××××××××××××××××
页: [1]
查看完整版本: 【6】MongoDB数据导入和导出