mongodb 匯入匯出

來源:互聯網
上載者:User

標籤:style   os   io   檔案   資料   for   ar   art   

(mongoexport匯出工具

MongoDB提供了mongoexport工具,可以把一個collection匯出成json格式或csv格式的檔案。可以指定匯出哪些資料項目,也可以根據給定的條件匯出資料。工具協助資訊如下:


[[email protected] bin]# ./mongoexport --help  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 ( /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   --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 if dbpath specified, each db is in a separate   directory   -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   [[email protected] bin]#

下面我們將以一個實際的例子說明,此工具的用法:

將foo庫中的表t1匯出成json格式:

[[email protected] bin]# ./mongoexport -d foo -c t1 -o /data/t1.json   connected to: 127.0.0.1   exported 1 records   [[email protected] bin]#



匯出成功後我們看一下/data/t1.json檔案的樣式,是否是我們所希望的:

[[email protected] data]# more t1.json   { "_id" : { "$oid" : "4f927e2385b7a6814a0540a0" }, "age" : 2 }   [[email protected] data]#



通過以上說明匯出成功,但有一個問題,要是異構資料庫的遷移怎麼辦呢?例如我們要將MongoDB的資料匯入到MySQL該怎麼辦呢?MongoDB提供了一種csv的匯出格式,就可以解決異構資料庫遷移的問題了. 下面將foo庫的t2表的age和name列匯出, 具體如下:

[[email protected] bin]# ./mongoexport -d foo -c t2 --csv -f age,name -o /data/t2.csv   connected to: 127.0.0.1   exported 1 records   [[email protected] bin]#

查看/data/t2.csv的匯出結果:

[[email protected] data]# more t2.csv   age,name   1,"wwl"   [[email protected] data]#



mongoimport匯入工具

MongoDB提供了mongoimport工具,可以把一個特定格式檔案中的內容匯入到某張collection中。工具協助資訊如下:

[[email protected] bin]# ./mongoimport --help   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 ( /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   --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 if dbpath specified, each db is in a separate   directory   -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 CSV,TSV only - use first line as headers   --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 4MB.



下面我們將以一人實際的例子說明,此工具的用法:
先看一下foo庫中的t1表資料:
> db.t1.find();   { "_id" : ObjectId("4f937a56450beadc560feaa9"), "age" : 5 }   >



t1其中有一條age=5的記錄, 我們再看一下json檔案中的資料是什麼樣子的:

[[email protected] data]# more t1.json   { "_id" : { "$oid" : "4f937a56450beadc560feaa7" }, "age" : 8 }   [[email protected] data]#



可以看到t1.json檔案中有一條age=8的資料,下面我們將用mongoimport工具將json檔案中的記錄匯入到t1表中:

[[email protected] bin]# ./mongoimport -d foo -c t1 /data/t1.json   connected to: 127.0.0.1   imported 1 objects



工具返回資訊說明向表中插入了一條記錄. 我們進庫裡實際驗證一下:

[[email protected] bin]# ./mongo   MongoDB shell version: 1.8.1   connecting to: test   > use foo   switched to db foo   > db.t1.find();   { "_id" : ObjectId("4f937a56450beadc560feaa9"), "age" : 5 }   { "_id" : ObjectId("4f937a56450beadc560feaa7"), "age" : 8 }   >





相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.