Mongo mongoexport/mongoimport介绍
一、Mongoexport导出数据
1,导出json数据
mongoexport -d db -c collection -o save-file.dat
2,导出CSV数据
mongoexport -d db -c collection --type=csv -f field1,field2 -o save-file.dat
说明:db目标数据库,collection目标集合,save-file.dat要保存的文件,--type指定导出的格式,默认是json,-f后面是需要导出的字段名称,多个字段名用逗号隔开
二、mongoimport导入数据
1,导入json数据
mongoimport -d db -c collection --file save-file.dat
2,导入CSV数据
mongoimport -d db -c collection --type=csv --headerline --file save-file.dat
说明:db目标数据库,collection目标集合,save-file.dat要导入的文件,--type指定导出的格式,默认是json,--headerline批明不导入第一行
三、导出工具mongoexport 简介,通过帮助先了解下mongoexport的功能参数

[root@iZ2ze4b308vd83fulq9n7iZ ~]# mongoexport --help
Usage:
mongoexport <options> Export data from MongoDB in CSV or JSON format. See http://docs.mongodb.org/manual/reference/program/mongoexport/ for more information. general options:
--help print usage
--version print the tool version and exit verbosity options:
-v, --verbose more detailed log output (include multiple times for more verbosity, e.g. -vvvvv)
--quiet hide all log output connection options:
-h, --host= mongodb host to connect to (setname/host1,host2 for replica sets)
--port= server port (can also use --host hostname:port) authentication options:
-u, --username= username for authentication
-p, --password= password for authentication
--authenticationDatabase= database that holds the user's credentials
--authenticationMechanism= authentication mechanism to use namespace options:
-d, --db= database to use
-c, --collection= collection to use output options:
-f, --fields= comma separated list of field names (required for exporting CSV) e.g. -f "name,age"
--fieldFile= file with field names - 1 per line
--type= the output format, either json or csv (defaults to '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 querying options:
-q, --query= query filter, as a JSON string, e.g., '{x:{$gt:1}}'
-k, --slaveOk allow secondary reads if available (default true)
--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}'

关键参数说明:
- -h,--host :代表远程连接的数据库地址,默认连接本地Mongo数据库;
- --port:代表远程连接的数据库的端口,默认连接的远程端口27017;
- -u,--username:代表连接远程数据库的账号,如果设置数据库的认证,需要指定用户账号;
- -p,--password:代表连接数据库的账号对应的密码;
- -d,--db:代表连接的数据库;
- -c,--collection:代表连接数据库中的集合;
- -f, --fields:代表集合中的字段,可以根据设置选择导出的字段;
- --type:代表导出输出的文件类型,包括csv和json文件;
- -o, --out:代表导出的文件名;
- -q, --query:代表查询条件;
- --skip:跳过指定数量的数据;
- --limit:读取指定数量的数据记录;
- --sort:对数据进行排序,可以通过参数指定排序的字段,并使用 1 和 -1 来指定排序的方式,其中 1 为升序排列,而-1是用于降序排列,如sort({KEY:1})。
注意:
当查询时同时使用sort,skip,limit,无论位置先后,最先执行顺序 sort再skip再limit。
实例:
首先查看下数据库中的数据一共多少条,通过的命令的方式查看下我们要导出的数据信息
> db.bike.find().count()
16878865
> db.bike.find({"source":"ofo"}).limit(1)
{ "_id" : ObjectId("59e8c27804390e04a063159d"), "lat" : 39.9571954199, "bikeId" : "pgdAVg", "current_time" : "2017-10-19 23:19:19", "source" : "ofo", "lng" : 116.3926501736 }
>
如何通过mongoexport导出"bikeId" : "pgdAVg"的数据,我导出了json和csv两种类型的数据;
#导出类型为json,数据库:mapdb,集合:bike 字段:bikeId,lat,lng,current_time,source ,条件为source字段为ofo第一条数据
mongoexport --port 27030 -u sa -p Expressin@0618 -d mapdb -c bike -f bikeId,lat,lng,current_time,source --type=json -o bike.csv --query='{"source":"ofo"}' --limit=1
#导出类型为csv,数据库:mapdb,集合:bike 字段:bikeId,lat,lng,current_time,source ,条件为source字段为ofo第一条数据
mongoexport --port 27030 -u sa -p Expressin@0618 -d mapdb -c bike -f bikeId,lat,lng,current_time,source --type=csv -o bike.csv --query='{"source":"ofo"}' --limit=1
查看下结果是否是我们两种不同的结果集;
1.导出csv类型;

2.导出json类型;

二、导入工具mongoimport 简介,通过帮助先了解下mongoimport的功能参数

[root@iZ2ze4b308vd83fulq9n7iZ ~]# mongoimport --help
Usage:
mongoimport <options> <file> Import CSV, TSV or JSON data into MongoDB. If no file is provided, mongoimport reads from stdin. See http://docs.mongodb.org/manual/reference/program/mongoimport/ for more information. general options:
--help print usage
--version print the tool version and exit verbosity options:
-v, --verbose more detailed log output (include multiple times for more verbosity, e.g. -vvvvv)
--quiet hide all log output connection options:
-h, --host= mongodb host to connect to (setname/host1,host2 for replica sets)
--port= server port (can also use --host hostname:port) authentication options:
-u, --username= username for authentication
-p, --password= password for authentication
--authenticationDatabase= database that holds the user's credentials
--authenticationMechanism= authentication mechanism to use namespace options:
-d, --db= database to use
-c, --collection= collection to use input options:
-f, --fields= comma separated list of field names, 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
--type= input format to import: json, csv, or tsv (defaults to 'json') ingest options:
--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)
--stopOnError stop importing at first insert/upsert error
--upsert insert or update objects that already exist
--upsertFields= comma-separated fields for the query part of the upsert
--writeConcern= write concern options e.g. --writeConcern majority, --writeConcern '{w: 3, wtimeout: 500, fsync: true, j: true}' (defaults to 'majority')

关键参数说明:
- h,--host :代表远程连接的数据库地址,默认连接本地Mongo数据库;
- --port:代表远程连接的数据库的端口,默认连接的远程端口27017;
- -u,--username:代表连接远程数据库的账号,如果设置数据库的认证,需要指定用户账号;
- -p,--password:代表连接数据库的账号对应的密码;
- -d,--db:代表连接的数据库;
- -c,--collection:代表连接数据库中的集合;
- -f, --fields:代表导入集合中的字段;
- --type:代表导入的文件类型,包括csv和json,tsv文件,默认json格式;
- --file:导入的文件名称
- --headerline:导入csv文件时,指明第一行是列名,不需要导入;
实例演示:

#首先查看集合中的数据
> db.bike_bak.find()
{ "_id" : ObjectId("59e8c27804390e04a063159d"), "lat" : 39.9571954199, "bikeId" : "pgdAVg", "current_time" : "2017-10-19 23:19:19", "source" : "ofo", "lng" : 116.3926501736 }
#删除集合中的数据
> db.bike_bak.remove({"bikeId" : "pgdAVg"})
WriteResult({ "nRemoved" : 1 })
#查看集合是否包含数据
> db.bike_bak.find()
>
#导入数据
[root@iZ2ze4b308vd83fulq9n7iZ ~]# mongoimport --port 27030 -u sa -p Expressin@0618 -d mapdb -c bike_bak --type=json --file bike.csv
2017-10-25T11:59:51.020+0800 connected to: localhost:27030
2017-10-25T11:59:51.030+0800 imported 1 document
#检查数据是否导入成功
> db.bike_bak.find()
{ "_id" : ObjectId("59e8c27804390e04a063159d"), "bikeId" : "pgdAVg", "current_time" : "2017-10-19 23:19:19", "lat" : 39.9571954199, "lng" : 116.3926501736, "source" : "ofo" }

测试过程中发现--headerline -f bikeId 同时存在会报错,提示不兼容,目前不知道如何处理。
整理自:
https://www.cnblogs.com/shelly520/p/9628790.html
https://www.cnblogs.com/mengyu/p/7718311.html
Mongo mongoexport/mongoimport介绍的更多相关文章
- Mongo 整体架构介绍(1)-------分片集群
摘要 在mongo初识文中介绍了mongo与cassandra的主要区别,以及mongo物理部署架构图.本文接着上一篇的mongo 架构图,来继续讲分片集群. 分片介绍 shard key mongo ...
- Mongo导出mongoexport和导入mongoimport介绍
最近爬取mobike和ofo单车数据,需要存储在csv文件中,因为设计的程序没有写存储csv文件的方法,为了偷懒所以就继续存储到了MongoDB中.恰好MongoDB支持导出的数据可以是csv文件和j ...
- Mongodb数据导出工具mongoexport和导入工具mongoimport介绍
一.导出工具mongoexport Mongodb中的mongoexport工具可以把一个collection导出成JSON格式或CSV格式的文件.可以通过参数指定导出的数据项,也可以根据指定的条件导 ...
- Mongodb数据导出工具mongoexport和导入工具mongoimport介绍(转)
原文地址:http://chenzhou123520.iteye.com/blog/1641319 一.导出工具mongoexport Mongodb中的mongoexport工具可以把一个colle ...
- Mongo db 简单介绍及命令笔记
首先来了解下什么是MongoDB ? MongoDB 是由C++语言编写的,是一个基于分布式文件存储的开源数据库系统. 在高负载的情况下,添加更多的节点,可以保证服务器性能. MongoDB 旨在为W ...
- mongodb数据导入导出mongoexport/mongoimport
数据导出 mongoexport 假设库里有一张user表,里面有2条记录,我们要将它导出 > use my_mongodb switched to db my_mongodb > db. ...
- MongoDB备份(mongoexport)与恢复(mongoimport)
1.备份恢复工具介绍: mongoexport/mongoimport mongodump/mongorestore(本文未涉及) 2.备份工具区别在哪里? 2.1 mongoexport/mongo ...
- Mongo--03 mongo副本集、备份与恢复
目录 一.mongo副本集配置 二.查看副本集状态 三.副本集权重调整 四.创建节点 五.仲裁节点 六.mongo备份与恢复 七.准备测试数据 一.mongo副本集配置 1.创建节点目录和数据目录 # ...
- mongo import excle
mongoimport --host 192.*******.** --db ** --collection ** --type csv --headerline --file /*****.cs ...
随机推荐
- C++标识符的作用域与可见性
一.标识符的作用域与可见性 作用域讨论的是标识符的有效范围,可见性讨论的是标识符是否可以被引用. 二.作用域 作用域是一个标识符在程序正文中有效的区域.C++中标识符的作用域有函数原型作用域.局部作用 ...
- SAS学习笔记51 SAS数据集
结构 SAS数据集是关系型的,包含两个部分:描述部分(变量)和数据部分(观察值) 形式 SAS系统中共有两种类型的数据集: 1.SAS 数据文件(SAS datafiles) 2.SAS 数据视窗(S ...
- Linux 查询端口被占用命令
1.lsof -i:端口号 用于查看某一端口的占用情况,比如查看8000端口使用情况,lsof -i:8000 lsof -i:8080:查看8080端口占用 lsof abc.txt:显示开启文件a ...
- Java8新特性 - Java内置的四大核心函数式接口
Java内置的四大核心函数式接口 Consumer:消费型接口 对类型为T的对象应用操作,包含方法:void accept(T t) public class TestLambda02 { publi ...
- win7实现tensorflow中的物体识别
实现条件: 1.win7 2.python 3.运行所需要的库:matplotlib.lxml.pillow.Cython 具体参考:https://github.com/tensorflow/mod ...
- CVE-2019-11604 Quest KACE Systems Management Appliance <= 9.0 XSS
CVE-2019-11604 Quest KACE Systems Management Appliance CVE-2019-11604 Quest KACE Systems Management ...
- ios设备app作为蓝牙外设端
苹果手机可以作为蓝牙外设端,被蓝牙中央端来扫描连接交互数据,实现模拟蓝牙外设硬件.通过阅读CoreBluetooth库,可以找到一个CBPeripheralManager的类,该类主要的作用就是允许你 ...
- ipv4与ipv6 Inet4Address类和Inet6Address类
在设置本地IP地址的时候,一些人会疑惑IPv4与IPv6的区别是什么?下面由学习啦小编为你分享ipv4与ipv6的区别的相关内容,希望对大家有所帮助. ipv4与ipv6的区别 在windows 7以 ...
- ELK文档--ELK简介
请参考:http://www.cnblogs.com/aresxin/p/8035137.html
- shiro系列五、shiro密码MD5加密
Shiro-密码的MD5加密 1.密码的加密 在数据表中存的密码不应该是123456,而应该是123456加密之后的字符串,而且还要求这个加密算法是不可逆的,即由加密后的字符串不能反推回来原来的密 ...