Mongodb提供了簡單易用的導出和導入命令。
一、導出命令的簡介與使用。
MongoDB提供了一個簡單的導出工具,這個工具位於{MongoDB_HOME}/bin/mongoexport.exe,如截圖(我的mongodb部署在windows操作系統中,在linux中類似):
下面介紹一下如何使用mongoexport導出命令:mongoexport -c gis -d local -o likehua.data
其中-c 表示幾何
-d 表示數據庫
-o 輸出文件名。
更多參數詳見:
Administrator@F523540 d:/Mongodb/bin
$ mongoexport --help
Export MongoDB data to CSV, TSV or JSON files.
options:
--help produce help message
-v [ --verbose ] be more verbose (include multiple times
for more verbosity e.g. -vvvvv)
--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
--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 directly
(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)
-f [ --fields ] arg comma separated list of field names
e.g. -f name,age
--fieldFile arg file with fields names - 1 per line
-q [ --query ] arg query filter, as a JSON string
--csv export to csv instead of json
-o [ --out ] arg output file; if not specified, stdout
is used
--jsonArray output to a json array rather than one
object per line
-k [ --slaveOk ] arg (=1) use secondaries for export if
available, default true
--forceTableScan force a table scan (do not use
$snapshot)
二、導入命令的簡介與使用。
導入命令的位置,如圖:
使用:
dministrator@F523540 d:/Mongodb/bin
mongoimport -d local -c gis --type json --file likehua.data -f username,_id
--type 導入的數據的數據類型
--file 導入的數據的文件名
-f 導入的字段
關於導入命令的更多參數說明如下:
Administrator@F523540 d:/Mongodb/bin
$ mongoimport --help
Import CSV, TSV or JSON data into MongoDB.
When importing JSON documents, each document must be a separate line of the inpu
t file.
Example:
mongoimport --host myhost --db my_cms --collection docs < mydocfile.json
options:
--help produce help message
-v [ --verbose ] be more verbose (include multiple times
for more verbosity e.g. -vvvvv)
--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
--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 directly
(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)
-f [ --fields ] arg comma separated 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 first line in input file is a header
(CSV and TSV only)
--upsert insert or update objects that already
exist
--upsertFields arg comma-separated fields for the query
part of the upsert. You should make
sure this is indexed
--stopOnError stop importing at first error rather
than continuing
--jsonArray load a json array, not one item per
line. Currently limited to 16MB.