mongodb 导出 带条件_MongoDB导入导出操作

MongoDB导出操作

MongoDB中的mongoexport工具可以把一个collection导出成JSON格式或者CSV格式的文件,速度非常快。

通过下面的代码,将miui_flow_statis库中的src_pv_uv集合导出到文本文件,在导出的文件中,每行一个JSON,代表一个Document。# mongoexport --host 10.136.33.51 --port 30000 -d miui_flow_statis -c src_pv_uv -o /tmp/src_pv_uv

2017-07-19T11:47:27.940+0800 connected to: 10.136.33.51:30000

2017-07-19T11:47:28.323+0800 exported 12740 records

MongoDB导入操作

MongoDB中的mongoimport工具可以把一个特定文件中的内容导入到指定的collection中。该工具可以导入JSON格式数据,也可以导入CSV格式数据。

通过下面的代码,将/tmp/src_pv_uv文件导入到miui_flow_statis库中的src_pv_uv集合。在/tmp/src_pv_uv文件中,每行一个JSON。# mongoimport --host 10.136.33.51 --port 30000 -d miui_flow_statis -c src_pv_uv /tmp/src_pv_uv

2017-07-19T11:56:44.147+0800 connected to: 10.136.33.51:30000

2017-07-19T11:56:44.955+0800 imported 12740 documents

MongoDB导出工具

mongoexport是Linux下的命令行工具,查看该命令的帮助文档可以看到有很多选项,我们重点分析一些常用的选项。

1, 指定主机和端口

指定MongoDB服务器IP地址。-h, --host=

(setname/host1,host2 for replica sets)

指定MongoDB服务器端口号(注意:-p是密码)。--port=

通常,在导出时指定服务器主机和端口号有以下几种用法# mongoexport -h host1:port

# mongoexport --host host1 --port 30000

# mongoexport --host replicaSetName/host1,host2 --port 3000

2, 指定用户名和密码

当MongoDB设置了账号认证时,需要在导出的时候提供用户名和密码。-u, --username= username for authentication

-p, --password= password for authentication

3, 指定数据库和集合名称

在使用mongoexport导出时,需要指定MongoDB的数据库名称和集合名称,可以通过下面两个选项设置。-d, --db= database to use

-c, --collection= collection to use

4, 指定输出文件

在使用mongoexport导出时,可以选择输出文件的类型(json、csv或控制台)。当选择输出为json类型文件时,可以设置输出每行一个json对象或一个json对象的数组,还可以对输出json进行格式化(pretty)。当选择输出为csv时,可以选择要输出的表头域,也可以省略表头域。默认输出的是json,每行一个json对象。当没有指定-o选项时,会将导出内容打印在控制台上。-f, --fields=[,]* csv header fields, eg: -f "name,age"

--fieldFile= file with field names - 1 per line

--type= 'json' or 'csv', default 'json'

-o, --out= output file; if not specified, stdout is used

--jsonArray output to a JSON array rather than one object per line

--pretty output JSON formatted to be human-readable

--noHeaderLine export CSV data without a list of field names at the first line

通常,在导出时可以有以下组合来指定导出文件的格式和内容。

导出为文件,格式为json,每行一个json对象。-o /tmp/data.txt

导出为文件,格式为json,构成一个json数组。-o /tmp/data.txt --jsonArray

导出为文件,格式为json,构成一个json数组,并进行格式化。-o /tmp/data.txt --jsonArray --pretty

导出为文件,格式为csv,指定表头域。-f "name,age"

5, 查询条件

在使用mongoexport导出数据时,可以指定查询语句或者指定一个包含查询语句的文件,为导出添加条件。-q, --query= query filter, as a JSON string, e.g., '{x:{$gt:1}}'

--queryFile= path to a file containing a query filter (JSON)

-k, --slaveOk allow secondary reads if available (default true) (default: false)

--readPreference=| specify either a preference name or a preference json object

--forceTableScan force a table scan (do not use $snapshot)

--skip= number of documents to skip

--limit= limit the number of documents to export

--sort= sort order, as a JSON string, e.g. '{x:1}'

--assertExists if specified, export fails if the collection does not exist (default: false)

MongoDB导入工具

mongoimport是Linux下的命令行工具,查看该命令的帮助文档可以看到有很多选项,我们重点分析一些常用的选项。

1, 指定主机和端口

指定MongoDB服务器IP地址。-h, --host=

(setname/host1,host2 for replica sets)

指定MongoDB服务器端口号(注意:-p是密码)。--port=

通常,在导入时指定服务器主机和端口号有以下几种用法# mongoimport -h host1:port

# mongoimport --host host1 --port 30000

# mongoimport --host replicaSetName/host1,host2 --port 3000

2, 指定用户名和密码

当MongoDB设置了账号认证时,需要在导出的时候提供用户名和密码。-u, --username= username for authentication

-p, --password= password for authentication

3, 指定数据库和集合名称

在使用mongoimport导入时,需要指定MongoDB的数据库名称和集合名称,可以通过下面两个选项设置。-d, --db= database to use

-c, --collection= collection to use

4, 指定输入文件

使用mongoimport导入时需要指定输入文件,如果导入文件是csv格式,或者是jsonArray格式,则需要通过选项指定输入格式。-f, --fields=[,]* comma separated list of fields, e.g. -f name,age

--fieldFile= file with field names - 1 per line

--file= file to import from; if not specified, stdin is used

--headerline use first line in input source as the field list (CSV and TSV only)

--jsonArray treat input source as a JSON array

--parseGrace= controls behavior when type coercion fails - one of: autoCast, skipField, skipRow, stop (defaults to 'stop') (default: stop)

--type= input format to import: json, csv, or tsv (defaults to 'json') (default: json)

--columnsHaveTypes indicated that the field list (from --fields, --fieldsFile, or --headerline) specifies types; They must be in the form of '.()'. The type

can be one of: auto, binary, bool, date, date_go, date_ms, date_oracle, double, int32, int64, string. For each of the date types, the argument is a datetime

layout string. For the binary type, the argument can be one of: base32, base64, hex. All other types take an empty argument. Only valid for CSV and TSV

imports. e.g. zipcode.string(), thumbnail.binary(base64)

5, 输入选项

使用mongoimport导入时可以设置一些输入选项,比如在插入前是否要删除已经存在的集合,当发生错误时是否要继续等。--drop drop collection before inserting documents

--ignoreBlanks ignore fields with empty values in CSV and TSV

--maintainInsertionOrder insert documents in the order of their appearance in the input source

-j, --numInsertionWorkers= number of insert operations to run concurrently (defaults to 1) (default: 1)

--stopOnError stop importing at first insert/upsert error

--mode=[insert|upsert|merge] insert: insert only. upsert: insert or replace existing documents. merge: insert or modify existing documents. defaults to insert

--upsertFields=[,]* comma-separated fields for the query part when --mode is set to upsert or merge

--writeConcern= write concern options e.g. --writeConcern majority, --writeConcern '{w: 3, wtimeout: 500, fsync: true, j: true}' (defaults to 'majority') (default: majority)

--bypassDocumentValidation bypass document validation

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值