Reprinted: http://chenzhou123520.iteye.com/blog/1641319
I. Export tool mongoexport
MongoDB The mongoexport tool can export a collection into JSON or CSV formats. You can export data items by specifying parameters, or you can export data by specifying conditions. The specific usage of mongoexport is as follows:
- [root@localhost 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
- --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
- --journal enable 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 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
Description of parameters:
- h: data base Host IP
- u: Specify the username of the database
- p: Specify the password of the database
- d: Specify the name of the database
- c: Specify the name of collection
- f: Specifies which columns to export
- o: Specify the name of the file to export
- q: Indicate the filter conditions for the exported data
Example: There is a students collection in the test library with the following data:
- > db.students.find()
- { "_id" : ObjectId("5031143350f2481577ea81e5"), "classid" : 1, "age" : 20, "name" : "kobe" }
- { "_id" : ObjectId("5031144a50f2481577ea81e6"), "classid" : 1, "age" : 23, "name" : "nash" }
- { "_id" : ObjectId("5031145a50f2481577ea81e7"), "classid" : 2, "age" : 18, "name" : "james" }
- { "_id" : ObjectId("5031146a50f2481577ea81e8"), "classid" : 2, "age" : 19, "name" : "wade" }
- { "_id" : ObjectId("5031147450f2481577ea81e9"), "classid" : 2, "age" : 19, "name" : "bosh" }
- { "_id" : ObjectId("5031148650f2481577ea81ea"), "classid" : 2, "age" : 25, "name" : "allen" }
- { "_id" : ObjectId("5031149b50f2481577ea81eb"), "classid" : 1, "age" : 19, "name" : "howard" }
- { "_id" : ObjectId("503114a750f2481577ea81ec"), "classid" : 1, "age" : 22, "name" : "paul" }
- { "_id" : ObjectId("503114cd50f2481577ea81ed"), "classid" : 2, "age" : 24, "name" : "shane" }
As can be seen from the above, there are three fields in the document: classid, age, name.
1. Export data directly to files
- [root@localhost mongodb]# ./bin/mongoexport -d test -c students -o students.dat
- connected to: 127.0.0.1
- exported 9 records
When the command is executed, we use ll command to check it and find that a student. dat file is generated in the directory.
- -rw-r--r-- 1 root root 869 Aug 21 00:05 students.dat
See the file information, the details are as follows:
- [root@localhost mongodb]# cat students.dat
- { "_id" : { "$oid" : "5031143350f2481577ea81e5" }, "classid" : 1, "age" : 20, "name" : "kobe" }
- { "_id" : { "$oid" : "5031144a50f2481577ea81e6" }, "classid" : 1, "age" : 23, "name" : "nash" }
- { "_id" : { "$oid" : "5031145a50f2481577ea81e7" }, "classid" : 2, "age" : 18, "name" : "james" }
- { "_id" : { "$oid" : "5031146a50f2481577ea81e8" }, "classid" : 2, "age" : 19, "name" : "wade" }
- { "_id" : { "$oid" : "5031147450f2481577ea81e9" }, "classid" : 2, "age" : 19, "name" : "bosh" }
- { "_id" : { "$oid" : "5031148650f2481577ea81ea" }, "classid" : 2, "age" : 25, "name" : "allen" }
- { "_id" : { "$oid" : "5031149b50f2481577ea81eb" }, "classid" : 1, "age" : 19, "name" : "howard" }
- { "_id" : { "$oid" : "503114a750f2481577ea81ec" }, "classid" : 1, "age" : 22, "name" : "paul" }
- { "_id" : { "$oid" : "503114cd50f2481577ea81ed" }, "classid" : 2, "age" : 24, "name" : "shane" }
Description of parameters:
- d: The library specified for use, in this case test
- c: Specifies the set to be exported, in this case students
- o: Specify the name of the file to export, in this case students.dat
As can be seen from the above results, we export data in JSON format by default instead of displaying the specified export style. If we need to export data in CSV format, we need to use the -- CSV parameter, as follows:
- [root@localhost mongodb]# ./bin/mongoexport -d test -c students --csv -f classid,name,age -o students_csv.dat
- connected to: 127.0.0.1
- exported 9 records
- [root@localhost mongodb]# cat students_csv.dat
- classid,name,age
- 1.0,"kobe",20.0
- 1.0,"nash",23.0
- 2.0,"james",18.0
- 2.0,"wade",19.0
- 2.0,"bosh",19.0
- 2.0,"allen",25.0
- 1.0,"howard",19.0
- 1.0,"paul",22.0
- 2.0,"shane",24.0
- [root@localhost mongodb]#
Description of parameters:
- csv: specifies the format to be exported to CSV
- f: Indicates the need to export data for the three columns classid, name, age
As can be seen from the above results, mongoexport successfully exported the data to students_csv.dat file according to CSV format.
Import tool mongoimport
The mongoimport tool in Mongodb can import content from a specific format file into a specified collection. The tool can import data in JSON format or CSV format. 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 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
- --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
- --journal enable 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
- --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.
Description of parameters:
- h: Indicates the IP of the database host
- u: Specify the username of the database
- p: Specify the password of the database
- d: Specify the name of the database
- c: Specify the name of collection
- f: Specify which columns to import
Example: First delete the data in students and verify it
- > db.students.remove()
- > db.students.find()
- >
Then import the contents of the students.dat file exported above
- [root@localhost mongodb]# ./bin/mongoimport -d test -c students students.dat
- connected to: 127.0.0.1
- imported 9 objects
- [root@localhost mongodb]#
Description of parameters:
- d: Specify the database name, in this case test
- c: Specify the collection name, in this case students
students.dat: imported file name
Query the data in the students collection
- > db.students.find()
- { "_id" : ObjectId("5031143350f2481577ea81e5"), "classid" : 1, "age" : 20, "name" : "kobe" }
- { "_id" : ObjectId("5031144a50f2481577ea81e6"), "classid" : 1, "age" : 23, "name" : "nash" }
- { "_id" : ObjectId("5031145a50f2481577ea81e7"), "classid" : 2, "age" : 18, "name" : "james" }
- { "_id" : ObjectId("5031146a50f2481577ea81e8"), "classid" : 2, "age" : 19, "name" : "wade" }
- { "_id" : ObjectId("5031147450f2481577ea81e9"), "classid" : 2, "age" : 19, "name" : "bosh" }
- { "_id" : ObjectId("5031148650f2481577ea81ea"), "classid" : 2, "age" : 25, "name" : "allen" }
- { "_id" : ObjectId("5031149b50f2481577ea81eb"), "classid" : 1, "age" : 19, "name" : "howard" }
- { "_id" : ObjectId("503114a750f2481577ea81ec"), "classid" : 1, "age" : 22, "name" : "paul" }
- { "_id" : ObjectId("503114cd50f2481577ea81ed"), "classid" : 2, "age" : 24, "name" : "shane" }
- >
Demonstrate the success of data import
The above demonstrates the contents of the file imported in JSON format. If you want to import the contents of the CSV format file, you need to specify the import format through the -- type parameter, as follows:
Delete data first
- > db.students.remove()
- > db.students.find()
- >
Import the students_csv.dat file exported before
- [root@localhost mongodb]# ./bin/mongoimport -d test -c students --type csv --headerline --file students_csv.dat
- connected to: 127.0.0.1
- imported 10 objects
- [root@localhost mongodb]#
Description of parameters:
- type: Specifies the file format to be imported
- headerline: Specifies that the first row is a column name and does not need to be imported
- file: Specifies the file to be imported
Query the students collection to verify that the import was successful:
Explain that the import has been successful.
- > db.students.find()
- { "_id" : ObjectId("503266029355c632cd118ad8"), "classid" : 1, "name" : "kobe", "age" : 20 }
- { "_id" : ObjectId("503266029355c632cd118ad9"), "classid" : 1, "name" : "nash", "age" : 23 }
- { "_id" : ObjectId("503266029355c632cd118ada"), "classid" : 2, "name" : "james", "age" : 18 }
- { "_id" : ObjectId("503266029355c632cd118adb"), "classid" : 2, "name" : "wade", "age" : 19 }
- { "_id" : ObjectId("503266029355c632cd118adc"), "classid" : 2, "name" : "bosh", "age" : 19 }
- { "_id" : ObjectId("503266029355c632cd118add"), "classid" : 2, "name" : "allen", "age" : 25 }
- { "_id" : ObjectId("503266029355c632cd118ade"), "classid" : 1, "name" : "howard", "age" : 19 }
- { "_id" : ObjectId("503266029355c632cd118adf"), "classid" : 1, "name" : "paul", "age" : 22 }
- { "_id" : ObjectId("503266029355c632cd118ae0"), "classid" : 2, "name" : "shane", "age" : 24 }
- >