Deep analysis of import and export of MongoDB data _mongodb

Source: Internet
Author: User
Tags mongodb first row server port

First, MongoDB export tool Mongoexport

The Mongoexport tool in MongoDB can export a collection to a file in JSON format or CSV format. You can specify the exported data items through parameters, or you can export the data based on the criteria you specify.
Mongoexport Specific usage

 [Root@localhost mongodb]#./bin/mongoexport--help Export MongoDB data to CSV, TSV or JSON files.  
              Options:--help produce help message-v [--verbose] is more verbose (include multiple verbosity e.g.-vvvvv)--version Print the program ' s version and exit-h [--host] Arg mong o 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 us  
              ername-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's data directory, so cannot is us Ed if a mongod is currently accessing the same path--DIRECTORYPERDB if dbpath specified         In a separate directory--journalEnable journaling-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 Li 

 ne-k [--slaveok] arg (=1) use secondaries for export if available, default True

Parameter description
-H: Indicates the IP of the database host
-U: Indicates the user name of the database
-P: Indicates the password for the database
-D: Indicates the name of the database
-C: Specify the name of collection
-F: Indicates which columns to export
-O: Indicates the file name to export
-Q: Indicates the filter condition for the exported data

Ii. examples of common data export

1. Direct export of data to a file

Copy Code code as follows:

[Root@localhost bin]#./mongoexport-d my_mongodb-c user-o User.dat
Connected to:127.0.0.1
Exported 2 records
[Root@localhost bin]# Cat User.dat
{"_id": {"$oid": "4F81A4A1779282CA68FD8A5A"}, "UID": 2, "username": "Jerry", "Age": 100}
{"_id": {"$oid": "4f844d1847d25a9ce5f120c4"}, "UID": 1, "username": "Tom", "Age": 25}
[Root@localhost bin]#

When the command is finished, the command is used to view it, and a Students.dat file is generated in the directory.

Parameter description

-D indicates the library used, in this case, "My_mongodb"
-C indicates the table to be exported, in this case "user"
-O indicates the name of the file to export, in this case, "User.dat"
Using the JSON style from the way you can see the export

2. Export the table T1 in the Foo Library to JSON format

Copy Code code as follows:

[Root@localhost bin]#./mongoexport-d foo-c t1-o/data/t1.json
Connected to:127.0.0.1
Exported 1 records
[Root@localhost bin]#

After exporting successfully, let's look at the style of the/data/t1.json file.

Copy Code code as follows:

Root@localhost data]# More T1.json
{"_id": {"$oid": "4f927e2385b7a6814a0540a0"}, "Age": 2}
[Root@localhost data]#

3, exported to CSV format data

Copy Code code as follows:

[Root@localhost bin]#./mongoexport-d foo-c T2--csv-f Age,name-o/data/t2.csv
Connected to:127.0.0.1
Exported 1 records
[Root@localhost bin]#

View export results for/data/t2.csv

Copy Code code as follows:

[Root@localhost data]# more T2.csv
Age,name
1, "WWL"
[Root@localhost data]#

Three, MongoDB import tool Mongoexport

The Mongoimport tool in MongoDB can import the contents of a particular format file into the specified collection. The tool can import JSON-formatted data, or you can import CSV-formatted data.

The specific use is as follows

 [Root@localhost mongodb]#./bin/mongoimport--help Options:--help produce help message-v [--verbose] Be more verbose (include multiple times for more verbosity e.g.-vvvvv)--version Print the Progr       Am ' 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 user  
             name-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's data directory, so cannot is used If a mongod is currently accessing the same path--directoryperdb if dbpath specified, each db is in a SE Parate directory--journal enable journaling-d [--DB] arg database to use-c [--collec tion] arG Collection to use (some commands)-F [--fields] arg comma separated list of field names e.g.-F name,age--field File arg file with fields Names-1/line--ignoreblanks if given, empty fields in CSV and TSV would be ignore D--type arg type of file to import. Default:json (JSON,CSV,TSV)--file arg file to import from; If not specified stdin are used--drop drop collection A--headerline csv,tsv only-use a s headers--upsert insert or update objects that already exist--upsertfields arg comma-separated fields for The query part of the Upsert. You are should make sure the this is indexed--stoponerror stop importing at the "a" rather than continuing Ay load a JSON array, not one item/line. 
 Currently limited to 4MB.

Parameter description

-H: Indicates the IP of the database host
-U: Indicates the user name of the database
-P: Indicates the password for the database
-D: Indicates the name of the database
-C: Specify the name of collection
-F: Indicates which columns to import

Iv. Common Data Import examples

1. Import JSON data

Copy Code code as follows:

[Root@localhost mongodb]#./bin/mongoimport-d test-c Students Students.dat
Connected to:127.0.0.1
Imported 9 objects
[Root@localhost mongodb]#

Parameter description
-D: Indicates the name of the database, in this case test
-C: Indicates collection name, in this case students
Students.dat: Imported file name

2, import the contents of CSV format file

Copy Code code as follows:

[Root@localhost mongodb]#./bin/mongoimport-d test-c students--type csv--headerline--file students_csv.dat
Connected to:127.0.0.1
Imported Objects
[Root@localhost mongodb]#

Parameter description:
-type: Indicates the file format to import
-headerline: Indicates that the first row is a column name and does not need to be imported
-file: Indicates the file to import

The above mentioned is the entire content of this article, I hope you can enjoy.

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.