发布于 2015-09-14 15:02:19 | 472 次阅读 | 评论: 0 | 来源: 网络整理
The mongoimport tool provides a route to import content from a JSON, CSV, or TSV export created by mongoexport, or potentially, another third-party export tool. See the “数据导入和导出” document for a more in depth usage overview, and the “mongoexport” document for more information regarding mongoexport, which provides the inverse “importing” capability.
注解
Do not use mongoimport and mongoexport for full instance, production backups because they will not reliably capture data type information. Use mongodump and mongorestore as described in “系统备份策略” for this kind of functionality.
Returns a basic help and usage text.
Increases the amount of internal reporting returned on the command line. Increase the verbosity with the -v form by including the option multiple times, (e.g. -vvvvv.)
Returns the version of the mongoimport program.
Specifies a resolvable hostname for the mongod to which you want to restore the database. By default mongoimport will attempt to connect to a MongoDB process ruining on the localhost port numbered 27017.
Optionally, specify a port number to connect a MongoDB instance running on a port other than 27017.
To connect to a replica set, use the --host argument with a setname, followed by a slash and a comma-separated list of host and port names. mongoimport will, given the seed of at least one connected set member, connect to primary node of that set. This option would resemble:
--host repl0/mongo0.example.net,mongo0.example.net:27018,mongo1.example.net,mongo2.example.net
You can always connect directly to a single MongoDB instance by specifying the host and port number directly.
Specifies the port number, if the MongoDB instance is not running on the standard port. (i.e. 27017) You may also specify a port number using the mongoimport --host command.
Enables IPv6 support that allows mongoimport to connect to the MongoDB instance using an IPv6 network. All MongoDB programs and processes, including mongoimport, disable IPv6 support by default.
Specifies a username to authenticate to the MongoDB instance, if your database requires authentication. Use in conjunction with the mongoimport --password option to supply a password.
Specifies a password to authenticate to the MongoDB instance. Use in conjunction with the mongoimport --username option to supply a username.
If you specify a --username without the --password option, mongoimport will prompt for a password interactively.
Specifies the directory of the MongoDB data files. If used, the --dbpath option enables mongoimport to attach directly to local data files and insert the data without the mongod. To run with --dbpath, mongoimport needs to lock access to the data directory: as a result, no mongod can access the same path while the process runs.
Use the --directoryperdb in conjunction with the corresponding option to mongod, which allows mongoimport to import data into MongoDB instances that have every database’s files saved in discrete directories on the disk. This option is only relevant when specifying the --dbpath option.
Allows mongoexport write to the durability journal to ensure that the data files will remain in a consistent state during the write process. This option is only relevant when specifying the --dbpath option.
Use the --db option to specify a database for mongoimport to restore data. If you do not specify a <db>, mongoimport creates new databases that correspond to the databases where data originated and data may be overwritten. Use this option to restore data into a MongoDB instance that already has data, or to restore only some data in the specified backup.
Use the --collection option to specify a collection for mongorestore to restore. If you do not specify a <collection>, mongoimport imports all collections created. Existing data may be overwritten. Use this option to restore data into a MongoDB instance that already has data, or to restore only some data in the specified imported data set.
Specify a field or number fields to import from the specified file. All other fields present in the export will be excluded during importation. Comma separate a list of fields to limit the fields imported.
As an alternative to “mongoimport --fields” the --fieldFile option allows you to specify a file (e.g. <file>`) to hold a list of field names to specify a list of fields to include in the export. All other fields will be excluded from the export. Place one field per line.
In csv and tsv exports, ignore empty fields. If not specified, mongoimport creates fields without values in imported documents.
Declare the type of export format to import. The default format is JSON, but it’s possible to import csv and tsv files.
Specify the location of a file containing the data to import. mongoimport will read data from standard input (e.g. “stdin.”) if you do not specify a file.
Modifies the importation procedure so that the target instance drops every collection before restoring the collection from the dumped backup.
If using “--type csv” or “--type tsv,” use the first line as field names. Otherwise, mongoimport will import the first line as a distinct document.
Modifies the import process to update existing objects in the database if they match an imported object, while inserting all other objects.
If you do not specify a field or fields using the --upsertFields mongoimport will upsert on the basis of the _id field.
Specifies a list of fields for the query portion of the upsert. Use this option if the _id fields in the existing documents don’t match the field in the document, but another field or field combination can uniquely identify documents as a basis for performing upsert operations.
To ensure adequate performance, indexes should exist for this field or fields.
2.2 新版功能.
Forces mongoimport to halt the import operation at the first error rather than continuing the operation despite errors.
在 2.2 版更改: The limit on document size increased from 4MB to 16MB.
Accept import of data expressed with multiple MongoDB document within a single JSON array.
Use in conjunction with mongoexport --jsonArray to import data written as a single JSON array. Limited to imports of 16 MB or smaller.
In this example, mongoimport imports the csv formatted data in the /opt/backups/contacts.csv into the collection contacts in the users database on the MongoDB instance running on the localhost port numbered 27017.
mongoimport --db users --collection contacts --type csv --file /opt/backups/contacts.csv
In the following example, mongoimport imports the data in the JSON formatted file contacts.json into the collection contacts on the MongoDB instance running on the localhost port number 27017. Journaling is explicitly enabled.
mongoimport --collection contacts --file contacts.json --journal
In the next example, mongoimport takes data passed to it on standard input (i.e. with a | pipe.) and imports it into the collection contacts in the sales database is the MongoDB datafiles located at /srv/mongodb/. if the import process encounters an error, the mongoimport will halt because of the --stopOnError option.
mongoimport --db sales --collection contacts --stopOnError --dbpath /srv/mongodb/
In the final example, mongoimport imports data from the file /opt/backups/mdb1-examplenet.json into the collection contacts within the database marketing on a remote MongoDB database. This mongoimport accesses the mongod instance running on the host mongodb1.example.net over port 37017, which requires the username user and the password pass.
mongoimport --host mongodb1.example.net --port 37017 --username user --password pass --collection contacts --db marketing --file /opt/backups/mdb1-examplenet.json