MongoDB Import Export

Source: Internet
Author: User
Tags server port

(Mongoexport Export Tool

MongoDB provides a mongoexport tool that can export a collection to a file in JSON or CSV format. You can specify which data items to export, or you can export data based on a given condition. Tool Help information is as follows:


[[email protected] bin]#./mongoexport--help Options:--help produce help message-v [--verbose] being more Verbo SE (include multiple times for more verbosity e.g.-VVVVV)-H [--host] arg MONGO host to connect to (/S1,S2 for set s)--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 Connecti Ng to a mongod server-needs to lock the data directory, so cannot being used if a mongod is currently accessing the SAM  E path--directoryperdb If dbpath specified, each db was 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 are used--jsonarray output to a JSON array rather than one object per line [[Email pro Tected] bin]#

below we will illustrate with a practical example the use of this tool:

Export the table T1 in Foo Library to JSON format:

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



After the export is successful, let's look at the style of the/data/t1.json file, whether it is what we want:

[[email protected] data]# more T1.json   {"_id": {"$oid": "4f927e2385b7a6814a0540a0"}, "Age": 2}   [email prot Ected] data]#



Export success through the above instructions, but there is a problem, what if the migration of heterogeneous databases? For example, what do we do to import MongoDB data into MySQL? MONGODB provides a CSV export format to solve the problem of heterogeneous database migration. The following is an export of the age and name columns for the T2 table of the Foo Library, as follows:

[Email protected] bin]#/mongoexport-d foo-c T2--csv-f age,name-o/data/t2.csv connected   to:127.0.0.1   Expo rted 1 Records   [[email protected] bin]#

To view/data/t2.csv's export results:

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



Mongoimport Import Tool

MongoDB provides a mongoimport tool that can import content from a particular format file into a single collection. Tool Help information is as follows:

[[email protected] bin]#./mongoimport--help Options:--help produce help message-v [--verbose] being more ver Bose (include multiple times for more verbosity e.g.-VVVVV)-H [--host] arg MONGO host to connect to (/S1,S2 for S ETS)--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 Connecti Ng to a mongod server-needs to lock the data directory, so cannot being used if a mongod is currently accessing the SAM  E path--directoryperdb If dbpath specified, each db was 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'll is ignored--type arg type of file to import. Default:json (JSON,CSV,TSV)--file arg file to import from; If not specified stdin was 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 lo Ad a JSON array, not one item per line. Currently limited to 4MB.



Here's a practical example of how this tool is used:
First look at the T1 table data in the Foo library:
> Db.t1.find ();   {"_id": ObjectId ("4f937a56450beadc560feaa9"), "Age": 5}   >



T1 one of the Age=5 records, let's look at what the data in the JSON file looks like:

[[email protected] data]# more T1.json   {"_id": {"$oid": "4f937a56450beadc560feaa7"}, "Age": 8}   [Email prot Ected] data]#



You can see a age=8 of data in the T1.json file, and we'll use the Mongoimport tool to import the records from the JSON file into the T1 table:

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



The tool return information description inserts a record into the table. Let's go into the library and actually verify that:

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





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.