需求介绍:将mongodb中的数据以文件的方式导出:json或cvs格式

mongo 提供了mongoexport的工具,可以实现将库中的数据以json或cvs的格式输出到文件中。mongoexport位于mongo安装位置中的bin/目录下。

mongoexport具体用法如下所示:

1. 使用help查看参数说明

E:\data
λ 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}'

2. 用例说明:

将info库中student的所有信息以json格式导出到student_json.dat数据文件中

mongoexport -h 127.0.0.1 -u root -p 12345 -d info -c student --type=json -o E:\data\student_json.dat

输出:

{
"id": 123,
"name": "张三",
"age": 12
}
{
"id": 124,
"name": "李四",
"age": 15
}

将info库中student的id,name信息以json格式导出到student_json.dat数据文件中,并且限定“行数”是1

mongoexport -h 127.0.0.1 -u root -p 12345 -d info -c student --type=json -f id,name --limit=1 -o E:\data\student_json.dat

输出:

{
"id": 1,
"name": "张三"
}

将info库中student的所有信息以cvs格式导出到student_cvs.dat数据文件中

mongoexport -h 127.0.0.1 -u root -p 12345 -d info -c student --type=cvs  -o E:\data\student_cvs.dat

输出:

123,"张三",12
124,"李四",15

将info库student“表”的name=张三的信息以cvs格式导出到student_cvs.dat数据文件中

mongoexport -h 127.0.0.1 -u root -p 12345 -d info -c student --type=cvs -q{"name":"张三"} -o E:\data\student_cvs.dat

输出:

123,"张三",12

注意:

a. --type=json 只是控制每一条“记录”是json格式,而整体的输出文件不是json 。如果想要控制整体的文件数据格式是json数组,则需要使用--jsonArray 参数控制

扩展:

导入工具mongoimport

Mongodb中的mongoimport工具可以把一个特定格式文件中的内容导入到指定的collection中。该工具可以导入JSON格式数据,也可以导入CSV格式数据。具体使用如下所示:

E:\data
λ 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:指明数据库宿主机的IP

-u:指明数据库的用户名

-p:指明数据库的密码

-d:指明数据库的名字

-c:指明collection的名字

-f:指明要导入那些列

示例:先删除students中的数据,并验证

> db.students.remove()
> db.students.find()
>
然后再导入上面导出的students.dat文件中的内容

mongoimport -d test -c students students.dat
connected to: 127.0.0.1
imported 9 objects

参数说明:

-d:指明数据库名,本例中为test

-c:指明collection名,本例中为students

students.dat:导入的文件名

查询students集合中的数据

> db.students.find()
{ "_id" : ObjectId("5031143350f2481577ea81e5"), "classid" : 1, "age" : 20, "name" : "kobe" }
{ "_id" : ObjectId("5031144a50f2481577ea81e6"), "classid" : 1, "age" : 23, "name" : "nash" }
{ "_id" : ObjectId("5031145a50f2481577ea81e7"), "classid" : 2, "age" : 18, "name" : "james" }
{ "_id" : ObjectId("5031146a50f2481577ea81e8"), "classid" : 2, "age" : 19, "name" : "wade" }
{ "_id" : ObjectId("5031147450f2481577ea81e9"), "classid" : 2, "age" : 19, "name" : "bosh" }
{ "_id" : ObjectId("5031148650f2481577ea81ea"), "classid" : 2, "age" : 25, "name" : "allen" }
{ "_id" : ObjectId("5031149b50f2481577ea81eb"), "classid" : 1, "age" : 19, "name" : "howard" }
{ "_id" : ObjectId("503114a750f2481577ea81ec"), "classid" : 1, "age" : 22, "name" : "paul" }
{ "_id" : ObjectId("503114cd50f2481577ea81ed"), "classid" : 2, "age" : 24, "name" : "shane" }
>

证明数据导入成功

上面演示的是导入JSON格式的文件中的内容,如果要导入CSV格式文件中的内容,则需要通过--type参数指定导入格式,具体如下所示:

先删除数据

> db.students.remove()
> db.students.find()
>

再导入之前导出的students_csv.dat文件

mongoimport -d test -c students --type csv --headerline --file students_csv.dat
connected to: 127.0.0.1
imported 10 objects

参数说明:

-type:指明要导入的文件格式

-headerline:指明第一行是列名,不需要导入

-file:指明要导入的文件

查询students集合,验证导入是否成功:

> db.students.find()
{ "_id" : ObjectId("503266029355c632cd118ad8"), "classid" : 1, "name" : "kobe", "age" : 20 }
{ "_id" : ObjectId("503266029355c632cd118ad9"), "classid" : 1, "name" : "nash", "age" : 23 }
{ "_id" : ObjectId("503266029355c632cd118ada"), "classid" : 2, "name" : "james", "age" : 18 }
{ "_id" : ObjectId("503266029355c632cd118adb"), "classid" : 2, "name" : "wade", "age" : 19 }
{ "_id" : ObjectId("503266029355c632cd118adc"), "classid" : 2, "name" : "bosh", "age" : 19 }
{ "_id" : ObjectId("503266029355c632cd118add"), "classid" : 2, "name" : "allen", "age" : 25 }
{ "_id" : ObjectId("503266029355c632cd118ade"), "classid" : 1, "name" : "howard", "age" : 19 }
{ "_id" : ObjectId("503266029355c632cd118adf"), "classid" : 1, "name" : "paul", "age" : 22 }
{ "_id" : ObjectId("503266029355c632cd118ae0"), "classid" : 2, "name" : "shane", "age" : 24 }
>

说明已经导入成功

Mongo的导出工具mongoexport介绍的更多相关文章

  1. Mongodb数据导出工具mongoexport和导入工具mongoimport介绍

    一.导出工具mongoexport Mongodb中的mongoexport工具可以把一个collection导出成JSON格式或CSV格式的文件.可以通过参数指定导出的数据项,也可以根据指定的条件导 ...

  2. Mongodb数据导出工具mongoexport和导入工具mongoimport介绍(转)

    原文地址:http://chenzhou123520.iteye.com/blog/1641319 一.导出工具mongoexport Mongodb中的mongoexport工具可以把一个colle ...

  3. Mongodb数据导出工具mongoexport和导入工具mongoimport使用

    如图所示,两个工具位于mongodb安装目录的bin目录下 下面介绍一下两者的使用方法: 一.导出工具mongoexport Mongodb中的mongoexport工具可以把一个collection ...

  4. 用Python编写博客导出工具

    用Python编写博客导出工具 罗朝辉 (http://kesalin.github.io/) CC 许可,转载请注明出处   写在前面的话 我在 github 上用 octopress 搭建了个人博 ...

  5. Mysql导入导出工具Mysqldump和Source命令用法详解

    Mysql本身提供了命令行导出工具Mysqldump和Mysql Source导入命令进行SQL数据导入导出工作,通过Mysql命令行导出工具Mysqldump命令能够将Mysql数据导出为文本格式( ...

  6. [转]Mysql导入导出工具Mysqldump和Source命令用法详解

    Mysql本身提供了命令行导出工具Mysqldump和Mysql Source导入命令进行SQL数据导入导出工作,通过Mysql命令行导出工具Mysqldump命令能够将Mysql数据导出为文本格式( ...

  7. 2.3MySQL 自带工具使用介绍

    1.mysql 首先看看“-e, --execute=name”参数,这个参数是告诉mysql,我只要执行“-e”后面的某个命令,而不是要通过mysql 连接登录到MySQL Server 上面.此参 ...

  8. EXCEL导出工具类及调用

    一.Excel导出工具类代码 package com.qiyuan.util; import java.io.OutputStream; import java.io.UnsupportedEncod ...

  9. mongo导入导出命令

    1.导出工具:mongoexport     1.概念:         mongoDB中的mongoexport工具可以把一个collection导出成JSON格式或CSV格式的文件.可以通过参数指 ...

随机推荐

  1. ArcGIS 在地图上添加标注

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. Orcle数据库恢复

    不知道什么原因,服务器上的数据库报错:ORA-01033:ORACLE initialization or shutdown in progress 首先检查:监听文件的主机名及端口号是否更改 数据文 ...

  3. DedeCMS文章标题前增加所属栏目名称链接

    问题描述:在调用文章标题的时候,我想实现这样的形式:“[国内新闻]站长无忧真的是一个不错的站”,其中“国内新闻”是标题的所属栏目,并且加链接:    解决方法: [field:typelink /]这 ...

  4. servlet HttpSession 监听器

    一.Servlet中对象作用域 1. ServletContext 上下文 应用服务器一启动就产生该对象,服务器关闭即销毁 作用于全局,所有Servlet ,相当于静态变量 2. HttpSessio ...

  5. 使用原生JS编写ajax操作XMLHttpRequst对象

    ajax其本质就是XMLHttpRequest,现在jquery调用异步的方法很方便,但是也不能忘记原生的JS去编写ajax; 需要注意的是,很多人在写的时候喜欢只用XMLHttpRequest对象r ...

  6. 为什么JS动态生成的input标签在后台有时候没法获取到

    最近在做一个产品添加的功能,需求有点奇葩,所以在添加的时候免不了要用到动态生成控件,然后我就使用了JS去动态生成一些 checkbox类型的input标签,在以前用asp.net在后台生成的input ...

  7. OC - 14.NSOperation与NSOperationQueue

    简介 通过NSOperation与NSOperationQueue的组合也能实现多线程 通常将任务封装成NSOperation对象,并将对象添加到NSOperationQueue中实现 NSOpera ...

  8. copy和mutableCopy的深、浅拷贝

    对象拷贝主要由两种方式:copy和mutableCopy.浅拷贝是指直接将指针指向原有的地址,从而达到复制的目的.深拷贝是指重新生成一个对象,将原有对象的内容复制到新的对象中.copy 返回的是一个不 ...

  9. Sql Server内置函数实现MD5加密

    实例 MD5加密“123456”: HashBytes('MD5','123456') 结果:0xE10ADC3949BA59ABBE56E057F20F883E (提示:看完最后,结果要进行转换.) ...

  10. Köln-keith jarrett

    在火车上遇到一男生,带着他弟弟.他弟弟跑来跑去的,他就安稳地坐在下铺看书,不知道是哪种语言. 我们都是在北京下车. 第二天在王府井吃饭,姐姐带我吃西餐.我又看到他,跟一个阿姨一起吃饭. 吃饭的时候姐姐 ...