このドキュメントは、mongoimport のバージョン 100.18.0 用です。
Synopsis
mongoimport imports content from an Extended JSON, CSV, or TSV export created by mongoexport, or potentially, another third-party export tool.
Run mongoimport from the system command line, not the mongo shell.
mongoexport はデータのエクスポート機能を備えています。
MongoDB データベースツールを使用して、セルフホスト型配置から MongoDB Atlas へ移行できます。MongoDB Atlas は、MongoDB をクラウドに配置するためのフルマネージド サービスです。詳細については、mongorestore でのシード処理を参照してください。
MongoDB Atlas へ移行するすべての方法については、「データの移行またはインポート」を参照してください。
Tip
グラフィカル ユーザー インターフェイス ツール MongoDB Compass を使用してデータをインポートおよびエクスポートすることもできます。詳細については、「MongoDB Compass のインポートとエクスポート」を参照してください。
構文
mongoimport 構文:
mongoimport <options> <connection-string> <file>
オプション
--quiet出力量を制限する quiet モードで
mongoimportを実行します。このオプションにより次の項目が抑制されます。
データベース コマンド からの出力
レプリケーション アクティビティ
接続を受け付けたイベント
接続を終了したイベント
--config=<filename>バージョン100.3.0の新機能。
次の
mongoimportオプションの機密値を含む YAML 構成ファイルへのフル パスを指定します:これは、
mongoimportにパスワードを指定する際に、パスワード プロンプトを使用して指定する以外で推奨される方法です。ファイル内の引数は任意に組み合わせて使用することができます。構成ファイルは以下の形式をとります。
password: <password> uri: mongodb://mongodb0.example.com:27017 sslPEMKeyPassword: <password> uriを指定せずにpasswordオプションを指定する場合は、mongoimportコマンドラインオプションを使用して、--usernameや--hostなど、接続文字列の他のコンポーネントを指定できます。このファイルは、必ず適切なファイルシステム権限で保護してください。
重要
--configオプションを使用する際には、次の制限事項と動作にご注意ください。passwordフィールドを指定し、競合するパスワードを含む接続文字列をuriフィールドに指定した場合、mongorestoreはエラーをスローします。--configを使用して構成ファイルを指定し、かつ--password、--uri、または--sslPEMKeyPasswordmongoimportコマンドラインオプションも使用している場合、コマンドラインオプションは対応する構成ファイルオプションを上書きします。
--uri=<connectionString>MongoDB 配置の解決可能な URI 接続文字列 を引用符で囲んで指定します。
--uri "mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]" 注意
If you don't specify a database with the
--dbor--urioption,mongoimportuses thetestdatabase by default. If thetestdatabase doesn't exist,mongoimportcreates the database.Starting with version
100.0ofmongoimport, the connection string may alternatively be provided as a positional parameter, without using the--urioption:mongoimport mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]] 接続文字列は、位置指定パラメータとして
mongodb://またはmongodb+srv://で始まっている限り、コマンドラインの任意の点で指定できます。以下に例を挙げます。mongoimport --username joe --password secret1 mongodb://mongodb0.example.com:27017 --ssl Only one connection string can be provided. Attempting to include more than one, whether using the
--urioption or as a positional argument, will result in an error.接続文字列のコンポーネントの詳細については、「接続文字列 URI 形式」ドキュメントを参照してください。
注意
connection string内の一部のコンポーネントは、--usernameや--passwordなど、独自の明示的コマンドライン オプションを使用して指定することもできます。明示的オプションを使用中に接続文字列を入力し、競合する情報を指定すると、エラーが発生します。注意
If using
mongoimporton Ubuntu 18.04, you may experience acannot unmarshal DNSerror message when using SRV connection strings (in the formmongodb+srv://) with the--urioption. If so, use one of the following options instead:the
--urioption with a non-SRV connection string (in the formmongodb://)直接接続するホストを指定するための
--hostオプション
--host=<hostname><:port>, -h=<hostname><:port>デフォルト: localhost:27017
MongoDB 配置の解決可能なホスト名を指定します。デフォルトでは、
mongoimportによってポート番号27017のローカルホストで実行されている MongoDB インスタンスに接続しようとします。To connect to a replica set, specify the
replSetNameand a seed list of set members, as in the following:--host=<replSetName>/<hostname1><:port>,<hostname2><:port>,<...> レプリカセット リスト形式を指定する場合、
mongoimportは常にプライマリに接続します。また、レプリカセットの任意の 1 つのノードに接続するには、そのノードのみのホストとポートを指定します。
--host=<hostname1><:port> IPv6 を使用し、
<address>:<port>形式を使用する場合は、アドレスとポートの組み合わせの一部を括弧で囲む必要があります(例:[<address>])。あるいは、
URI connection stringでホスト名を直接指定することもできます。--hostを使用中に接続文字列を入力し、競合する情報を指定すると、エラーが発生します。
--port=<port>デフォルト: 27017
MongoDB インスタンスがクライアント接続のためにリッスンする TCP ポートを指定します。
あるいは、
URI connection stringでポートを直接指定することもできます。接続文字列を提供すると同時に、--portを使用中に競合する情報を指定すると、エラーが発生します。
--sslEnables connection to a
mongodormongosthat has TLS/SSL support enabled.あるいは、
URI connection stringで TLS/SSL サポートを直接構成することもできます。--sslを使用中に接続文字列を入力し、競合する情報を指定すると、エラーが発生します。TLS/SSL と MongoDB の詳細については、「TLS/SSL 用に mongod と mongos を構成する」と「クライアントの TLS/SSL 構成」を参照してください。
--sslCAFile=<filename>証明書認証機関からのルート証明書チェーンを含む
.pemファイルを指定します。相対パスまたは絶対パスを使用して.pemファイルのファイル名を指定します。あるいは、
URI connection stringで.pemファイルを直接指定することもできます。--sslCAFileを使用中に接続文字列を入力し、競合する情報を指定すると、エラーが発生します。TLS/SSL と MongoDB の詳細については、「TLS/SSL 用に mongod と mongos を構成する」と「クライアントの TLS/SSL 構成」を参照してください。
--sslPEMKeyFile=<filename>TLS/SSL 証明書とキーの両方を含む
.pemファイルを指定します。相対パスまたは絶対パスを使用して、.pemファイルのファイル名を指定します。This option is required when using the
--ssloption to connect to amongodormongosthat hasCAFileenabled withoutallowConnectionsWithoutCertificates.あるいは、
URI connection stringで.pemファイルを直接指定することもできます。--sslPEMKeyFileを使用中に接続文字列を入力し、競合する情報を指定すると、エラーが発生します。TLS/SSL と MongoDB の詳細については、「TLS/SSL 用に mongod と mongos を構成する」と「クライアントの TLS/SSL 構成」を参照してください。
--sslPEMKeyPassword=<value>Specifies the password to de-crypt the certificate-key file (i.e.
--sslPEMKeyFile). Use the--sslPEMKeyPasswordoption only if the certificate-key file is encrypted. In all cases, themongoimportredacts the password from all logging and reporting output.If the private key in the PEM file is encrypted and you do not specify the
--sslPEMKeyPasswordoption, themongoimportprompts for a passphrase. See TLS/SSL Certificate Passphrase.あるいは、
URI connection stringでパスワードを直接指定することもできます。--sslPEMKeyPasswordを使用中に接続文字列を入力し、競合する情報を指定すると、エラーが発生します。TLS/SSL と MongoDB の詳細については、「TLS/SSL 用に mongod と mongos を構成する」と「クライアントの TLS/SSL 構成」を参照してください。
警告
一部のシステムでは、
--sslPEMKeyPasswordオプションを使用して直接提供されたパスワードは、他のユーザーによって呼び出される可能性のあるpsなどのシステム ステータス プログラムに表示される場合があります。代わりに--configオプションを使用して、パスワードを含む構成ファイルを指定することを検討してください。
--sslCRLFile=<filename>証明書失効リストを含む
.pemファイルを指定します。相対パスまたは絶対パスを使用して.pemファイルのファイル名を指定します。TLS/SSL と MongoDB の詳細については、「TLS/SSL 用に mongod と mongos を構成する」と「クライアントの TLS/SSL 構成」を参照してください。
--sslAllowInvalidCertificatesBypasses the validation checks for server certificates and allows the use of invalid certificates. When using the
allowInvalidCertificatessetting, MongoDB logs a warning for invalid certificates.警告
--sslAllowInvalidCertificatesオプションは使用可能ですが、可能な限り使用しないでください。--sslAllowInvalidCertificatesの使用が必要な場合は、侵入が不可能なシステムでのみこのオプションを使用してください。Connecting to a
mongodormongosinstance without validating server certificates is a potential security risk. If you only need to disable the validation of the hostname in the TLS/SSL certificates, see--sslAllowInvalidHostnames.あるいは、
URI connection stringで証明書の検証を直接無効にすることもできます。接続文字列を提供すると同時に、--sslAllowInvalidCertificatesを使用中に競合する情報を指定すると、エラーが発生します。TLS/SSL と MongoDB の詳細については、「TLS/SSL 用に mongod と mongos を構成する」と「クライアントの TLS/SSL 構成」を参照してください。
--sslAllowInvalidHostnamesTLS/SSL 証明書のホスト名の検証を無効にします。証明書内のホスト名が指定されたホスト名と一致しない場合でも、
mongoimportによって MongoDB インスタンスへの接続が許可されます。あるいは、
URI connection stringでホスト名の検証を直接無効にすることもできます。--sslAllowInvalidHostnamesを使用中に接続文字列を入力し、競合する情報を指定すると、エラーが発生します。TLS/SSL と MongoDB の詳細については、「TLS/SSL 用に mongod と mongos を構成する」と「クライアントの TLS/SSL 構成」を参照してください。
--username=<username>, -u=<username>Specifies a username with which to authenticate to a MongoDB database that uses authentication. Use in conjunction with the
--passwordand--authenticationDatabaseoptions.あるいは、
URI connection stringでユーザー名を直接指定することもできます。--usernameを使用中に接続文字列を入力し、競合する情報を指定すると、エラーが発生します。If connecting to a MongoDB Atlas cluster using the
MONGODB-AWSauthentication mechanism, you can specify your AWS access key ID in:このフィールド
the
connection string, orAWS_ACCESS_KEY_ID環境変数。
それぞれの例については、「AWS IAM 認証情報を使用した MongoDB Atlas クラスターへの接続」を参照してください。
--password=<password>, -p=<password>Specifies a password with which to authenticate to a MongoDB database that uses authentication. Use in conjunction with the
--usernameand--authenticationDatabaseoptions.To prompt the user for the password, pass the
--usernameoption without--passwordor specify an empty string as the--passwordvalue, as in--password "".あるいは、
URI connection stringでパスワードを直接指定することもできます。--passwordを使用中に接続文字列を入力し、競合する情報を指定すると、エラーが発生します。If connecting to a MongoDB Atlas cluster using the
MONGODB-AWSauthentication mechanism, you can specify your AWS secret access key in:このフィールド
the
connection string, orAWS_SECRET_ACCESS_KEY環境変数。
それぞれの例については、「AWS IAM 認証情報を使用した MongoDB Atlas クラスターへの接続」を参照してください。
警告
一部のシステムでは、
--passwordオプションを使用して直接提供されたパスワードは、他のユーザーによって呼び出される可能性のあるpsなどのシステム ステータス プログラムに表示される場合があります。そのため、次の代替策を検討しましょう。--passwordオプションを省略し、インタラクティブなパスワード プロンプトを表示する--configオプションを使用して、パスワードを含む構成ファイルを指定します。
--awsSessionToken=<AWS Session Token>If connecting to a MongoDB Atlas cluster using the
MONGODB-AWSauthentication mechanism, and using session tokens in addition to your AWS access key ID and secret access key, you can specify your AWS session token in:このフィールド
the
AWS_SESSION_TOKENauthMechanismPropertiesparameter to theconnection string, orAWS_SESSION_TOKEN環境変数。
それぞれの例については、「AWS IAM 認証情報を使用した MongoDB Atlas クラスターへの接続」を参照してください。
Only valid when using the
MONGODB-AWSauthentication mechanism.
--authenticationDatabase=<dbname>Specifies the authentication database where the specified
--usernamehas been created. See Authentication Database.If you use the GSSAPI (Kerberos), PLAIN (LDAP SASL), or
MONGODB-AWSauthentication mechanisms, you must set--authenticationDatabaseto$external.あるいは、
URI connection stringで認証データベースを直接指定することもできます。--authenticationDatabaseを使用中に接続文字列を入力し、競合する情報を指定すると、エラーが発生します。
--authenticationMechanism=<name>デフォルト: SCRAM-SHA-1
Specifies the authentication mechanism the
mongoimportinstance uses to authenticate to themongodormongos.Changed in version 100.1.0:
Starting in version
100.1.0,mongoimportadds support for theMONGODB-AWSauthentication mechanism when connecting to a MongoDB Atlas cluster.値説明RFC 5802 standard Salted Challenge Response Authentication Mechanism using the SHA-1 hash function.
RFC 7677 standard Salted Challenge Response Authentication Mechanism using the SHA-256 hash function.
featureCompatibilityVersion を
4.0に設定する必要があります。MongoDB TLS/SSL 証明書認証。
MONGODB-AWSMongoDB Atlas クラスターへの接続に使用する AWS IAM 認証情報を使用した外部認証。「AWS IAM 認証情報を使用して MongoDB Atlas クラスターに接続する」を参照してください。
バージョン100.1.0の新機能。
GSSAPI(Kerberos)
Kerberos を使用する外部認証。このメカニズムは MongoDB Enterprise でのみ使用できます。
PLAIN(LDAP SASL)
LDAP を使用する外部認証。データベース内のユーザー認証には、
PLAINを使用することもできます。PLAINはパスワードをプレーン テキストで送信します。このメカニズムは MongoDB Enterprise でのみ使用できます。あるいは、
URI connection stringで認証メカニズムを直接指定することもできます。--authenticationMechanismを使用中に接続文字列を入力し、競合する情報を指定すると、エラーが発生します。
--gssapiServiceName=<serviceName>Specify the name of the service using GSSAPI/Kerberos. Only required if the service does not use the default name of
mongodb.このオプションは MongoDB Enterprise でのみ使用できます。
--gssapiHostName=<hostname>GSSAPI/Kerberos を使用してサービスのホスト名を指定します。マシンのホスト名が DNS で解決されたホスト名と一致しない場合にのみ必要となります。
このオプションは MongoDB Enterprise でのみ使用できます。
--db=<database>, -d=<database>mongoimportを実行するデータベースの名前を指定します。あるいは、
URI connection stringでデータベースを直接指定することもできます。--dbを使用中に接続文字列を入力し、競合する情報を指定すると、エラーが発生します。
--collection=<collection>, -c=<collection>Specifies the collection to import. If you do not specify
--collection,mongoimportreads the collection name from the input filename, omitting the file's extension if it has one.
--fields=<field1[,field2]>, -f=<field1[,field2]>ファイルの最初の行(ヘッダー)にフィールド名がない CSV または TSV ファイルをインポートする場合は、フィールド名をカンマで区切って指定します。
To also specify the field type as well as the field name, use
--fieldswith--columnsHaveTypes.If you attempt to include
--fieldswhen importing JSON data,mongoimportreturns an error.--fieldsis only for CSV or TSV imports.
--fieldFile=<filename>As an alternative to
--fields, the--fieldFileoption allows you to specify a file that holds a list of field names if your CSV or TSV file does not include field names in the first line of the file (i.e. header). Place one field per line.To also specify the field type as well as the field name, use
--fieldFilewith--columnsHaveTypes.If you attempt to include
--fieldFilewhen importing JSON data,mongoimportreturns an error.--fieldFileis only for CSV or TSV imports.
--ignoreBlanksIgnores empty fields in CSV and TSV exports. If not specified,
mongoimportcreates fields without values in imported documents.If you attempt to include
--ignoreBlankswhen importing JSON data,mongoimportreturns an error.--ignoreBlanksis only for CSV or TSV imports.
--type=<json|csv|tsv>インポートするファイルの種類を指定します。デフォルト形式は JSON ですが、CSV と TSV ファイルをインポートすることも可能です。
csvパーサーは、 RFC- に準拠するデータを受け入れるため、バックスラッシュは有効なエスケープ文字では4180 ありません 。 CSV データのフィールドを二重引用符で囲む場合、別の二重引用符を前に追加することで、内部二重引用符をエスケープする必要があります。
--file=<filename>インポートするデータを含むファイルの場所と名前を指定します。ファイルを指定しない場合、
mongoimportは標準入力(例: 「stdin」)からデータを読み取ります。
--drop入力からデータをインポートする前に、ターゲット インスタンスがコレクションと関連する インデックス を削除するようにインポート プロセスを変更します。
--headerlineIf using
--type csvor--type tsv, uses the first line as field names. Otherwise,mongoimportimports the first line as a distinct document.If you attempt to include
--headerlinewhen importing JSON data,mongoimportreturns an error.--headerlineis only for CSV or TSV imports.
--useArrayIndexFieldsバージョン100.0.0の新機能。
CSV または TSV ファイルのインポート時に、フィールド内の自然数は配列インデックスと解釈されます。
フィールド名は
<colName>.<arrayIndex>形式にします。arrayIndexには0で始まり、配列のノードごとに1ずつ順番に増加する自然数を入れます。以下は CSV ファイルの例です。
a.0,a.1,a.2,a.3 red,yellow,green,blue An import with the
--useArrayIndexFieldsoption would result in the following document:"a" : [ "red", "yellow", "green", "blue" ] If using the
--columnsHaveTypesoption as well, use the form<colName>.<arrayIndex>.<type>(<arg>)to specify both the array index and type for each field. See--columnsHaveTypesfor more information.先頭にゼロがある数字キー(例:
a.000,a.001)は、配列インデックスとして解釈されません。キーの最初の部分が自然数(例:
0.a,1.a)の場合は、配列インデックスとしてでなく、ドキュメントのキーとして解釈されます。If using the
--ignoreBlanksoption with--useArrayIndexFields,mongoimportreturns an error if you attempt to import a document that contains a blank value (e.g."") for an array index field.The
--useArrayIndexFieldsoption has no effect when importing JSON data, as arrays are already encoded in JSON format.
--mode=<insert|upsert|merge|delete>Default: insert
インポート ファイル内のドキュメントと一致するデータベース内の既存ドキュメントをインポートプロセスがどのように取り扱うかを指定します。
By default,
mongoimportuses the_idfield to match documents in the collection with documents in the import file. To specify the fields against which to match existing documents for theupsert,merge, anddeletemodes, use--upsertFields.値説明insertInsert the documents in the import file.
mongoimportreturns an error if you attempt to import a document that contains a duplicate value for a field with a unique index, such as_id.upsertReplace existing documents in the database with matching documents from the import file.
mongoimportinserts all other documents. Replace Matching Documents during Import describes how to use--modeupsert.mergeMerge existing documents that match a document in the import file with the new document.
mongoimportinserts all other documents. Merge Matching Documents during Import describes how to use--modemerge.deleteDelete existing documents in the database that match a document in the import file.
mongoimporttakes no action on non-matching documents. Delete Matching Documents describes how to use--modedelete.バージョン100.0.0の新機能。
--upsertFields=<field1[,field2]>Specifies a list of fields for the query portion of the import process.
--upsertFieldscan be used with--modeupsert,merge, anddelete.このオプションは、既存ドキュメントの
_idフィールドがドキュメントのフィールドと一致せず、かつ別のフィールドまたはフィールドの組み合わせによってアップサート操作の実行基準としてドキュメントを一意に識別できる場合に使用します。If you do not specify a field,
--upsertFieldsupserts on the basis of the_idfield.To ensure adequate performance, indexes should exist for the field or fields you specify with
--upsertFields.
--stopOnErrorエラーが生じても操作を続行するのでなく、最初のエラー時に
mongoimportが挿入操作を停止するよう強制します。By default,
mongoimportcontinues an operation when it encounters duplicate key and document validation errors. To ensure that the program stops on these errors, specify--stopOnError.
--jsonArray単一のJSON配列内に複数のMongoDBドキュメントで表現されたデータのインポートを受け入れます。インポートは 16MB 以下に制限されます。
Use
--jsonArrayin conjunction withmongoexport --jsonArray.
--legacyインポート データが、デフォルトの拡張JSON v1 形式でなく、 拡張JSON v2 形式であることを示します。
Tip
一般に、
mongoexportとmongoimportのバージョンは一致する必要があります。具体的には、mongoexportから作成されたデータをインポートするには、対応するバージョンのmongoimportを使用する必要があります。たとえば、インポート データが v1 形式の場合は次のようになります。
{"_id":1.0,"myregfield":{"$regex":"foo","$options":"i"}} Import without the
--legacyoption results in the following document in the collection:{ "_id" : 1, "myregfield" : { "$regex" : "foo", "$options" : "i" } } Import with the
--legacyresults in the following document in the collection:{ "_id" : 1, "myregfield" : { "$regularExpression" : { "pattern" : "foo", "options" : "i" } } }
--maintainInsertionOrderデフォルト: false
指定した場合、
mongoimportは、入力ソースに出現する順序でドキュメントを挿入します。つまり、一括書き込み (write) バッチの順と、バッチ内のドキュメント順の両方が維持されます。Specifying
--maintainInsertionOrderalso enables--stopOnErrorand setsnumInsertionWorkersto 1.指定されていない場合、
mongoimportが、無作為な順序で挿入を実行する可能性があります。
--numInsertionWorkers=<int>デフォルト: 1
同時に実行するする挿入ワーカーの数を指定します。
大規模なインポートの場合、挿入ワーカーの数を増やすとインポートが迅速化する可能性があります。
--writeConcern=<document>デフォルト: 過半数
Specifies the write concern for each write operation that
mongoimportperforms.w オプション適用のドキュメントとしての書込み保証(write concern)の指定
--writeConcern "{w:'majority'}" If the write concern is also included in the
--uri connection string, the command-line--writeConcernoverrides the write concern specified in the URI string.
--bypassDocumentValidationmongoimportが操作中にドキュメントの検証をバイパスできるようにします。これにより、検証要件を満たさないドキュメントを挿入できるようになります。
--columnsHaveTypesInstructs
mongoimportthat the field list specified in--fields,--fieldFile, or--headerlinespecifies the types of each field.フィールド名の形式は
<colName>.<type>(<arg>)にする必要があります。(、)、\を引数に含める場合は、バックスラッシュでエスケープする必要があります。typeサポートされる引数ヘッダー フィールドの例auto()なし。
misc.auto()binary(<arg>)user thumbnail.binary(base64)boolean()なし。
verified.boolean()date(<arg>)date_go(<arg>)のエイリアス。Go 言語 time.ParseL 形式。created.date(2006-01-02 15:04:05)date_go(<arg>)created.date_go(2006-01-02T15:04:05Z)date_ms(<arg>)created.date_ms(yyyy-MM-dd H:mm:ss)date_oracle(<arg>)created.date_oracle(YYYY-MM-DD HH24:MI:SS)decimal()なし
price.decimal()double()なし。
revenue.double()int32()なし。
followerCount.int32()int64()なし。
bigNumber.int64()string()なし。
zipcode.string()使用例については、「指定されたフィールド タイプを含む CSV のインポート」を参照してください。
If you attempt to include
--columnsHaveTypeswhen importing JSON data,mongoimportreturns an error.--columnsHaveTypesis only for CSV or TSV imports.
--parseGrace=<grace>デフォルト: stop
Specifies how
mongoimporthandles type coercion failures when importing CSV or TSV files with--columnsHaveTypes.--parseGraceは JSON ドキュメントのインポート時には、効果がありません。値説明autoCastフィールドの値に基づいて型を割り当てます。たとえば、フィールドが
doubleとして定義され、そのフィールドの値が"foo"であった場合、mongoimportそのフィールド値は文字列になります。skipFieldmongoimportはインポートされている行のフィールドのタイプが予想と一致しない場合、そのフィールドを含めません。skipRowmongoimportは予想される型と一致しない型の値を含む行はインポートしません。stopmongoimportはエラーを返し、インポートを終了します。