对于 AI 代理:可在 https://www.mongodb.com/zh-cn/docs/llms.txt 获取文档索引—通过在任何 URL 路径后添加 .md 可获取所有页面的 Markdown 版本。
Docs 菜单

mongoexport

本文档适用于 mongoexport100.18.0 版本。

mongoexport 是一个数据库工具,它可将 MongoDB 实例中存储的数据导出为 JSON 或 CSV。

Run mongoexport from the system command line, not the mongo shell.

mongoexport 不是用于备份部署的工具。如果要创建备份解决方案,请参阅使用 MongoDB 工具备份和恢复。

提示

mongoimport 而它提供了相应的结构化数据 导入功能。

注意

如果要存档过时数据以节省存储成本,请考虑MongoDB Atlas中的 Online 存档 。 Online 存档会自动将不常访问的数据存档到完全托管的S3 存储桶,以实现经济高效的数据分层。

您可以使用 Database Tools 从自托管部署迁移到 MongoDB Atlas。MongoDB Atlas 是用于云中 MongoDB 部署的完全托管服务。要了解详情,请参阅使用 mongorestore 播种。

要了解迁移到 MongoDB Atlas 的所有方法,请参阅迁移或导入数据。

mongoexport 事务语法:

mongoexport --collection=<coll> <options> <connection-string>

You must specify the collection to export. If you do not specify an output file, mongoexport writes to the standard output (e.g. stdout).

要连接到端口 27017 上所运行的本地 MongoDB 实例,您不必指定主机或端口。

示例,要将指定的 从在端口collection output file上运行的本地MongoDB实例导出到指定的27017 :

mongoexport --collection=events --db=reporting --out=events.json

要指定 MongoDB 实例的主机和/或端口,可以执行以下任一操作:

  • Specify the hostname and port in the --uri connection string:

    mongoexport --uri="mongodb://mongodb0.example.com:27017/reporting" --collection=events --out=events.json [additional options]

    When using the --uri connection string, the database can be specified as part of the string.

  • Specify the hostname and port in the --host:

    mongoexport --host="mongodb0.example.com:27017" --collection=events --db=reporting --out=events.json [additional options]
  • Specify the hostname and port in the --host and --port:

    mongoexport --host="mongodb0.example.com" --port=27017 --collection=events --db=reporting --out=events.json [additional options]

有关可用选项的更多信息,请参阅选项

注意

mongoexport 不是用于备份数据的工具。要备份副本集,请参阅使用 MongoDB 工具备份和恢复。

要连接到副本集以导出其数据,可以执行以下任一操作:

  • Specify the replica set name and members in the --uri connection string:

    mongoexport --uri="mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/reporting?replicaSet=myReplicaSetName" --collection=events --out=events.json [additional options]

    When using the --uri connection string, the database can be specified as part of the string.

  • Specify the replica set name and members in the --host:

    mongoexport --host="myReplicaSetName/mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com" --collection=events --db=reporting --out=events.json [additional options]

默认情况下, mongoexport从副本集的主节点读取。 要覆盖默认值,可以指定读取偏好:

  • 可以在以下语句中指定读取偏好: --uri connection string

    mongoexport --uri="mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/reporting?replicaSet=myReplicaSetName&readPreference=secondary" --collection=events --out=events.json [additional options]

    If specifying the read preference tags, include the readPreferenceTags option:

    mongoexport --uri="mongodb://mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017/reporting?replicaSet=myReplicaSetName&readPreference=secondary&readPreferenceTags=region:east" --collection=events --out=events.json [additional options]

    When using the --uri connection string, the database can be specified as part of the string.

  • You can specify the read preference using the --readPreference command-line option. The command-line option takes a string if specifying only the read preference mode:

    mongoexport --host="myReplicaSetName/mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017" --readPreference=secondary --collection=events --db=reporting --out=events.json [additional options]

    或者,该命令行选项可使用用引号括起的文档 '{ mode: <mode>, tagSets: [ <tag1>, ... ], maxStalenessSeconds:<num>}' 来指定模式、可选读取偏好标签集和可选 maxStalenessSeconds

    mongoexport --host="myReplicaSetName/mongodb0.example.com:27017,mongodb1.example.com:27017,mongodb2.example.com:27017" --readPreference='{mode: "secondary", tagSets: [ { "region": "east" } ]}' --collection=events --db=reporting --out=events.json [additional options]

有关可用选项的更多信息,请参阅选项

注意

mongoexport 不是用于备份数据的工具。要备份分片集群,请参阅备份和恢复自管理分片集群。

要连接到分片集群以导出其数据,可以执行以下任一操作:

  • Specify the hostname of the mongos instance in the --uri connection string

    mongoexport --uri="mongodb://mongos0.example.com:27017/reporting" --collection=events --out=events.json [additional options]

    When using the --uri connection string, the database can be specified as part of the string.

  • Specify the hostname and port of the mongos instance in the --host

    mongoexport --host="mongos0.example.com:27017" --collection=events --db=reporting --out=events.json[additional options]

默认情况下,mongoexport 从分片副本集的主节点中读取。要覆盖默认值,可以指定读取偏好

  • 可以在以下语句中指定读取偏好: --uri connection string

    mongoexport --uri="mongodb://mongos0.example.com:27017/reporting?readPreference=secondary" --collection=events --out=events.json [additional options]

    If specifying the read preference tags, include the readPreferenceTags option:

    mongoexport --uri="mongodb://mongos0.example.com:27017/reporting?readPreference=secondary&readPreferenceTags=region:east" --collection=events --out=events.json [additional options]

    When using the --uri connection string, the database can be specified as part of the string.

  • You can specify the read preference using the --readPreference command-line option. The command-line option takes a string if specifying only the read preference mode:

    mongoexport --host="mongos0.example.com:27017" --readPreference=secondary --collection=events --db=reporting --out=events.json [additional options]

    或者,该命令行选项可使用用引号括起的文档 '{ mode: <mode>, tagSets: [ <tag1>, ... ], maxStalenessSeconds:<num>}' 来指定模式、可选读取偏好标签集和可选 maxStalenessSeconds

    mongoexport --host="mongos0.example.com:27017" --readPreference='{mode: "secondary", tagSets: [ { "region": "east" } ]}' --collection=events --db=reporting --out=events.json [additional options]
--help

返回有关 mongoexport 的选项和使用的信息。

--verbose, -v

增加标准输出或日志文件中返回的内部报告数量。通过包括多个 -v 选项来提高详细程度,(例如 -vvvvv。)

--quiet

在尝试限制输出量的安静模式下运行 mongoexport

该选项会抑制:

  • Output from database commands

  • 复制活动

  • 已接受和已关闭的事件连接

  • 所有日志,包括错误信息,解析选项时出现的错误信息除外

--version

返回 mongoexport 版本号。

--config=<filename>

100.3.0版本新增。

指定 YAML 配置文件的完整路径,该文件包含以下 mongoexport 选项的敏感值:

除通过密码提示来指定密码之外,这是为 mongoexport 指定密码的另一推荐方法。您可以在文件中使用任何参数的组合。

配置文件采用以下形式:

password: <password>
uri: mongodb://mongodb0.example.com:27017
sslPEMKeyPassword: <password>

如果您指定了 password 选项但未指定 uri,则可以使用 mongoexport 命令行选项指定连接字符串的其他组件,例如 --username--host

请务必使用相应的文件系统权限保护此文件。

重要

使用 --config 选项时,请注意以下限制和行为:

  • 如果您提供 password 字段,并在 uri 字段中提供一个与密码冲突的连接字符串,则 mongorestore 会抛出错误。

  • 如果您使用 --config 指定配置文件,并使用 --password--uri--sslPEMKeyPassword mongoexport命令行选项,则命令行选项将覆盖相应的配置文件选项。

--uri=<connectionString>

指定 MongoDB 部署的可解析 URI 连接字符串(用引号括起):

--uri="mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]"

Starting with version 100.0 of mongoexport, the connection string may alternatively be provided as a positional parameter, without using the --uri option:

mongoexport mongodb://[username:password@]host1[:port1][,host2[:port2],...[,hostN[:portN]]][/[database][?options]]

作为位置参数,可以在命令行上的任何位置指定连接字符串,前提是它以 mongodb://mongodb+srv:// 开头。例如:

mongoexport --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 --uri option or as a positional argument, will result in an error.

有关连接字符串的组件信息,请参阅连接字符串 URI 格式文档。

注意

connection string 中的某些组件也可以使用各自的显式命令行选项来指定,例如 --username--password。在提供连接字符串的同时,使用显式选项并指定冲突信息会导致错误。

注意

如果在Ubuntu 上使用mongoexport18.04 ,则在使用带有 选项的cannot unmarshal DNS SRV 连接字符串(采用mongodb+srv:// 形式)时,您可能会遇到--uri 错误消息。如果是这样,请改用以下选项之一:

警告

在某些系统上,使用 --uri 选项在连接字符串中提供的密码可能会对其他用户调用的系统状态程序(例如 ps)可见。请考虑:

  • 在连接字符串中省略密码,接收交互式密码提示,或者

  • 使用 --config 选项指定包含密码的配置文件。

--host=<hostname><:port>, -h=<hostname><:port>

默认:localhost:27017

指定 MongoDB 部署的可解析主机名。默认情况下,mongoexport 尝试连接到在本地主机端口号 27017 上运行的 MongoDB 实例。

To connect to a replica set, specify the replSetName and a seed list of set members, as in the following:

--host=<replSetName>/<hostname1><:port>,<hostname2><:port>,<...>

指定副本集列表格式时,mongoexport 始终连接到主节点

还可以通过仅指定该成员的主机和端口来连接至副本集的任何单个成员:

--host=<hostname1><:port>

如果您使用 IPv6 并采用 <address>:<port> 格式,则须将地址与端口的组合部分用方括号括起(即 [<address>])。

或者,也可直接在 URI connection string 中指定主机名。在使用 --host 的同时提供连接字符串并指定冲突信息将导致错误。

--port=<port>

默认值:27017

指定 MongoDB 实例侦听客户端连接的 TCP 端口。

或者,也可以直接在 URI connection string 中指定端口。在提供连接字符串的同时使用 --port 并指定冲突信息将导致错误。

--ssl

Enables connection to a mongod or mongos that 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 --ssl option to connect to a mongod or mongos that has CAFile enabled without allowConnectionsWithoutCertificates.

或者,也可以直接在 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 --sslPEMKeyPassword option only if the certificate-key file is encrypted. In all cases, the mongoexport will redact the password from all logging and reporting output.

If the private key in the PEM file is encrypted and you do not specify the --sslPEMKeyPassword option, the mongoexport will prompt 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 配置。

--sslAllowInvalidCertificates

Bypasses the validation checks for server certificates and allows the use of invalid certificates. When using the allowInvalidCertificates setting, MongoDB logs as a warning the use of the invalid certificate.

警告

尽管可用,但请尽可能避免使用 --sslAllowInvalidCertificates 选项。如果需要使用 --sslAllowInvalidCertificates,请仅在不可能入侵的系统上使用该选项。

Connecting to a mongod or mongos instance 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 配置。

--sslAllowInvalidHostnames

禁用 TLS/SSL 证书中的主机名验证。即使证书中的主机名与指定的主机名不匹配,也支持 mongoexport 连接到 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 --password and --authenticationDatabase options.

或者,也可直接在 URI connection string 中指定用户名。在使用 --username 的同时提供连接字符串并指定冲突信息将导致错误。

If connecting to a MongoDB Atlas cluster using the MONGODB-AWS authentication mechanism, you can specify your AWS access key ID in:

请参阅使用 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 --username and --authenticationDatabase options.

To prompt the user for the password, pass the --username option without --password or specify an empty string as the --password value, as in --password "" .

或者,也可以直接在 URI connection string 中指定密码。在提供连接字符串的同时使用 --password 并指定冲突信息将导致错误。

If connecting to a MongoDB Atlas cluster using the MONGODB-AWS authentication mechanism, you can specify your AWS secret access key in:

请参阅使用 AWS IAM 凭据连接到 MongoDB Atlas 集群,了解每个凭据的示例。

警告

在某些系统上,直接使用 --password 选项提供的密码,可能会被其他用户调用的系统状态程序(如 ps)探测到。请考虑:

  • 省略 --password 选项以接收交互式密码提示,或

  • 使用 --config 选项指定包含密码的配置文件。

--awsSessionToken=<AWS Session Token>

If connecting to a MongoDB Atlas cluster using the MONGODB-AWS authentication 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:

请参阅使用 AWS IAM 凭据连接到 MongoDB Atlas 集群,了解每个凭据的示例。

仅在使用 MONGODB-AWS authentication mechanism 时有效。

--authenticationDatabase=<dbname>

Specifies the authentication database where the specified --username has been created. See Authentication Database.

如果未指定身份验证数据库,mongoexport 将假定指定要导出的数据库保存用户凭证。

If you use the GSSAPI (Kerberos), PLAIN (LDAP SASL), or MONGODB-AWS authentication mechanisms, you must set --authenticationDatabase to $external.

或者,也可以直接在 URI connection string 中指定身份验证数据库。在提供连接字符串的同时使用 --authenticationDatabase 并指定冲突信息,将导致错误。

--authenticationMechanism=<name>

默认值:SCRAM-SHA-1

Specifies the authentication mechanism the mongoexport instance uses to authenticate to the mongod or mongos.

Changed in version 100.1.0:

从版本 100.1.0 开始,mongoexport 添加了在连接 MongoDB Atlas 集群时对 MONGODB-AWS 身份验证机制的支持。

说明

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-AWS

使用 AWS IAM 凭据进行外部身份验证,用于连接到 MongoDB Atlas 集群。请参阅使用 AWS IAM 凭证连接到 MongoDB Atlas 集群

100.1.0版本新增。

GSSAPI (Kerberos)

使用 Kerberos 的外部身份验证。此机制仅在 MongoDB Enterprise 中可用。

普通版 (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>

指定要在其上运行mongoexport的数据库的名称。

或者也可以直接在 URI connection string 中指定数据库。在提供连接字符串的同时使用 --db 并指定冲突信息将导致错误。

--collection=<collection>, -c=<collection>

指定要导出的集合。

--fields=<field1[,field2]>, -f=<field1[,field2]>

指定要包含在 导出中的一个或多个字段。使用逗号分隔的字段列表指定多个字段。

如果任一字段名称包含空格,请使用引号将字段列表括起来。例如,如果希望导出两个字段 phoneuser number,则应指定 --fields "phone,user number"

For csv output formats, mongoexport includes only the specified field(s), and the specified field(s) can be a field within a sub-document.

For JSON output formats, mongoexport includes only the specified field(s) and the _id field, and if the specified field(s) is a field within a sub-document, the mongoexport includes the sub-document with all its fields, not just the specified field within the document.

有关示例用法,请参阅:使用--fields选项以 CSV 格式导出数据

--fieldFile=<filename>

An alternative to --fields. The --fieldFile option allows you to specify in a file the field or fields to include in the export and is only valid with the --type option with value csv. The file must have only one field per line, and the line(s) must end with the LF character (0x0A).

mongoexport 仅包括指定的字段。 指定的字段可以是子文档中的字段。

有关示例用法,请参阅使用文件指定要以 CSV 格式导出的字段

--query=<JSON>, -q=<JSON>

以 JSON 文档(用引号括起)的形式提供查询,以在导出时返回匹配的文档。

必须用单引号 ('{ ... }') 括起查询文档,确保它不会与 shell 环境交互。

查询必须采用扩展JSON v2 格式(宽松模式或规范/严格模式),包括用引号将字段名称和操作符括起来:

例如,数据库 records 中名为 test 的集合包含以下文档:

{ "_id" : ObjectId("51f0188846a64a1ed98fde7c"), "a" : 1, "date" : ISODate("1960-05-01T00:00:00Z") }
{ "_id" : ObjectId("520e61b0c6646578e3661b59"), "a" : 1, "b" : 2, "date" : ISODate("1970-05-01T00:00:00Z") }
{ "_id" : ObjectId("520e642bb7fa4ea22d6b1871"), "a" : 2, "b" : 3, "c" : 5, "date" : ISODate("2010-05-01T00:00:00Z") }
{ "_id" : ObjectId("520e6431b7fa4ea22d6b1872"), "a" : 3, "b" : 3, "c" : 6, "date" : ISODate("2015-05-02T00:00:00Z") }
{ "_id" : ObjectId("520e6445b7fa4ea22d6b1873"), "a" : 5, "b" : 6, "c" : 8, "date" : ISODate("2018-03-01T00:00:00Z") }
{ "_id" : ObjectId("5cd0de910dbce4346295ae28"), "a" : 15, "b" : 5, "date" : ISODate("2015-03-01T00:00:00Z") }

以下mongoexport -qa$gte使用3 date$ltISODate("2016-01-01T00:00:00Z")选项,仅导出字段 大于或等于 ( ) 且字段 小于 ( ) ,它是使用日期2 ({ "$date": "YYYY-MM-DDTHH:mm:ss.mmm\<offset\>"} ) 的扩展JSON v 格式(宽松模式)指定的:

mongoexport -d=test -c=records -q='{ "a": { "$gte": 3 }, "date": { "$lt": { "$date": "2016-01-01T00:00:00.000Z" } } }' --out=exportdir/myRecords.json

生成的文件包含以下文档:

{"_id":{"$oid":"520e6431b7fa4ea22d6b1872"},"a":3.0,"b":3.0,"c":6.0,"date":{"$date":"2015-05-02T00:00:00Z"}}
{"_id":{"$oid":"5cd0de910dbce4346295ae28"},"a":15.0,"b":5.0,"date":{"$date":"2015-03-01T00:00:00Z"}}

You can sort the results with the --sort option to mongoexport.

从MongoDB9.0 开始,当路径的中间组件是标量值大量时,服务器将以不同方式将虚线字段路径与null 进行匹配。此更改会影响$eq$ne$in$nin 操作符。

例如,考虑包含文档 { "a": [ 1 ] } 的集合。该文档中不存在路径 a.b

  • 在 MongoDB 8.x 及更早版本中,谓词 { "a.b": { "$ne": null } } 匹配文档。

  • 从 MongoDB 9.0 开始,相同的谓词不匹配文档,而是由谓词 { "a.b": { "$eq": null } } 匹配。

如果您的查询使用此类谓词,则相同的 --query 值在 MongoDB 9.0 上选择的文档集与早期版本上选择的文档集不同。对 { "$ne": null } 进行过滤的操作返回的文档数量较少。在从 MongoDB 9.0 部署中转储或导出数据之前,请查看将点分路径与 null 进行比较的任何查询。确认查询仍然选择您预期的文档。

--queryFile=<filename>

--query--queryFile的替代方案。 选项允许您在文件中指定扩展JSON v 格式的查询。2

--type=<string>

Default: json

Specifies the file type to export. Specify csv for CSV format or json for JSON format.

If you specify csv, then you must also use either the --fields or the --fieldFile option to declare the fields to export from the collection.

--out=<file>, -o=<file>

指定要写入导出内容的文件。 如果未指定文件名, mongoexport会将数据写入标准输出(例如 stdout )。

--jsonFormat=<canonical|relaxed>

默认:宽松

修改输出以使用MongoDB扩展JSON (v)2 格式的规范或宽松模式。

有关规范模式和宽松模式之间的区别,请参阅MongoDB扩展JSON (v)。2

--jsonArray

Modifies the output of mongoexport to write the entire contents of the export as a single JSON array. By default mongoexport writes data using one JSON document for every MongoDB document.

--pretty

以美观打印格式的 JSON 输出文档。

--noHeaderLine

By default, mongoexport includes the exported field names as the first line in a CSV output. --noHeaderLine directs mongoexport to export the data without the list of field names. --noHeaderLine is only valid with the --type option with value csv.

有关示例用法,请参阅从 CSV 输出中排除字段名称

--readPreference=<string|document>

Default: primary

Specifies the read preference for mongoexport. The --readPreference option can take:

  • 如果仅指定读取偏好模式,则为一个字符串:

    --readPreference=secondary
  • 用引号括起的文档,用于指定模式、可选的读取偏好标签集以及可选的 maxStalenessSeconds:

    --readPreference='{mode: "secondary", tagSets: [ { "region": "east" } ], maxStalenessSeconds: 120}'

    If specifying the maxStalenessSeconds, the value must be greater than or equal to 90.

mongoexport defaults to primary read preference.

If the read preference is also included in the --uri connection string, the command-line --readPreference overrides the read preference specified in the URI string.

--skip=<number>

Use --skip to control where mongoexport begins exporting documents. See skip() for information about the underlying operation.

--limit=<number>

Specifies a maximum number of documents to include in the export. See limit() for information about the underlying operation.

--sort=<JSON>

指定导出结果的顺序。如果存在可以支持排序操作的索引,则结果必须小于 32 MB。

Use --sort conjunction with --skip and --limit to limit number of exported documents.

mongoexport -d=test -c=records --sort='{a: 1}' --limit=100 --out=export.0.json
mongoexport -d=test -c=records --sort='{a: 1}' --limit=100 --skip=100 --out=export.1.json
mongoexport -d=test -c=records --sort='{a: 1}' --limit=100 --skip=200 --out=export.2.json

See sort() for information about the underlying operation.

给本页内容打分