Mongo导出mongoexport和导入mongoimport介绍
最近爬取mobike和ofo单车数据,需要存储在csv文件中,因为设计的程序没有写存储csv文件的方法,为了偷懒所以就继续存储到了MongoDB中。恰好MongoDB支持导出的数据可以是csv文件和json文件,所以下面介绍下MongoDB mongoexport和mongoimport方法;
一、导出工具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 - 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()
> db.bike.find({"source":"ofo"}).limit()
{ "_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 -u sa -p Expressin@ -d mapdb -c bike -f bikeId,lat,lng,current_time,source --type=json -o bike.csv --query='{"source":"ofo"}' --limit=
#导出类型为csv,数据库:mapdb,集合:bike 字段:bikeId,lat,lng,current_time,source ,条件为source字段为ofo第一条数据
mongoexport --port -u sa -p Expressin@ -d mapdb -c bike -f bikeId,lat,lng,current_time,source --type=csv -o bike.csv --query='{"source":"ofo"}' --limit=
查看下结果是否是我们两种不同的结果集;
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 - 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 )
--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" : })
#查看集合是否包含数据
> db.bike_bak.find()
>
#导入数据
[root@iZ2ze4b308vd83fulq9n7iZ ~]# mongoimport --port -u sa -p Expressin@ -d mapdb -c bike_bak --type=json --file bike.csv
--25T11::51.020+ connected to: localhost:
--25T11::51.030+ imported 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 同时存在会报错,提示不兼容,目前不知道如何处理。
Mongo导出mongoexport和导入mongoimport介绍的更多相关文章
- MongoDB 备份(mongodump)恢复(mongorerstore) 导出 (Mongoexport) 导入( Mongoimport)
MongoDB 备份(mongodump) 在Mongodb中我们使用mongodump命令来备份MongoDB数据.该命令可以导出所有数据到指定目录中. mongodump命令可以通过参数指定导出的 ...
- mongodb数据导入导出mongoexport/mongoimport
数据导出 mongoexport 假设库里有一张user表,里面有2条记录,我们要将它导出 > use my_mongodb switched to db my_mongodb > db. ...
- C#可扩展编程之MEF学习笔记(二):MEF的导出(Export)和导入(Import)
上一篇学习完了MEF的基础知识,编写了一个简单的DEMO,接下来接着上篇的内容继续学习,如果没有看过上一篇的内容, 请阅读:http://www.cnblogs.com/yunfeifei/p/392 ...
- sqlloader导出数据和导入数据
分类: Oracle 忙了一天终于把sqlloader导出数据和导入数据弄清楚了,累死俺了... 这个总结主要分为三个大部分,第一部分(实例,主要分两步),第二部分(参数小总结),第三部分(完全参数总 ...
- 针对数据泵导出 (expdp) 和导入 (impdp)工具性能降低问题的检查表 (文档 ID 1549185.1)
针对数据泵导出 (expdp) 和导入 (impdp)工具性能降低问题的检查表 (文档 ID 1549185.1) 文档内容 适用于: Oracle Database – Enterprise Edi ...
- 动态链接库DLL导出函数并导入使用
动态链接库DLL导出函数并导入使用 本文完全参考自<vs2008制作dll笔记,回带值样例>. 首先制作DLL文件,在vs2010中新建Win32控制台项目,选择DLL选项,简历头文件,源 ...
- Oracle11g导出dmp并导入Oracle10g的操作记录
Oracle11g导出dmp并导入Oracle10g的操作记录. 操作环境说明: Oracle11g环境:Windows7,Oracle Database 11g Enterprise Edition ...
- 通过export方式导出,在导入时要加{ },export default则不需要
怎么就是记不住呢?? 通过export方式导出,在导入时要加{ },export default则不需要
- 使用PL/SQL连接oracle数据库,并将数据进行导出备份和导入恢复
使用PL/SQL连接oracle数据库,并将数据进行导出备份和导入恢复 这种操作百度一搜一大片,今天整理以前做的项目时自己备份了一下数据库,试着将数据进行导出备份和导入恢复了一下:下面是操作过程: 1 ...
随机推荐
- L322
As a nutritionist helping people shed pounds, I often recommend incorporating portion-controlled des ...
- 给定两个数组,这两个数组是排序好的,让你求这两个数组合到一起之后第K大的数。
题目:给定两个数组,这两个数组是排序好的,让你求这两个数组合到一起之后第K大的数. 解题思路: 首先取得数组a的中位数a[aMid],然后在b中二分查找a[aMid],得到b[bMid],b[bSt] ...
- synchronized(五)
package com.bjsxt.base.sync005;/** * synchronized的重入 * @author alienware * */public class SyncDubbo2 ...
- 1) 上传多张图片时 ,对 $_FILES 的处理. upload ; 2)fileinput 上传多张图片. 3) 修改,删除的时候删除原来的资源,图片 update, delete , 删除 4)生成器中两个字段上传图片的时候,要修改生成器生成的代码
1上传多张图片, 要对 $_FILES进行 重新处理. //添加 public function addCourseAlbumAction() { $CourseAlbumModel = new Co ...
- session_id 生成原理
PHPSESSID生成 生成规则是根据hash_func散列来生成的,相关的参数有: - 客户端IP - 当前时间(秒) - 当前时间(微妙) - PHP自带的随机数生产器 hash_func是php ...
- 复习回顾(String,StringBuffer,Arrays方法总结)
String: String类的对象是一经创建就无法变动内容的字符串常量,创建String类的对象可以使用直接赋值和利用构造方法赋值 String str=“hello”; String str=n ...
- JAVA_工具类01_ResourceBundle
在java.util.ResourceBundle包下,是java提供的工具类 ResourceBundle只能读取properties文件中的内容,可以使用ResourceBundle的静态方法来创 ...
- Linux命令--1
之前一直在学习Linux,不过有点一天打鱼两天晒网的意味,现在希望通过写博客的形式,积累更多的知识,也希望可以帮到同在linux坑中的各位小伙伴们~ PS:我的笔记重点在于通俗,很多命令一百度就有,但 ...
- Oracle账户管理
登录:conn 用户名/口令创建用户create user 用户名 identified by 口令修改密码password 用户名删除用户drop user 用户名如果要删除的用户已经创建表,那么再 ...
- php-fpm高并发配置[1000+]
Dell R430 2个物理CPU,每个CPU有6个内核: www.conf: pm = dynamic pm.max_children = 120 pm.start_servers = 8 pm.m ...