mongodb 导出一条数据_MongoDB数据库中数据的导出

1. 在mongodb安装目录的bin目录下有一个数据导出工具:mongoexport.exe,我们可以打开一个命令窗口,切换到该目录下并执行mongoexport,就可以看到使用该工具的很多参数说明。

假设mongodb的安装目录为D:\program\mongodb,那么可以通过以下方式了解导出工具的使用:

>d:

>D:\program\mongodb\bin

>mongoexport

--help                    produce help message

-v [ --verbose ]          be more verbose (include multiple time

verbosity e.g. -vvvvv)

--version                 print the program's version and exit

........

2. 常用数据导出方式

首先,在MongoDB中新增一个localdb,以及创建集合user并插入一些对象。

>use localdb

>db.user.insert({"name" : "ming", "age" : 2 })

>db.user.insert({"name" : "lisi", "age" : 20 })

那么,接下来就可以使用以下方式导出user表中的数据。其中,-d代表指明从哪个数据库中导出数据;-c代表从哪个集合中导出数据;-o指明导出的数据输出到哪个文件中。

>mongoexport -d localdb -c user -o user.dat

导出成功后,打开user.dat文件,导出的数据如下所示:

{ "_id" : { "$oid" : "4eed9f9ca939118694cf05e4" }, "name" : "ming", "age" : 2 }

{ "_id" : { "$oid" : "4ef1f3cf3bd18218e6bdfa31" }, "name" : "lisi", "age" : "20" }

3. 导出为CVS文件

如果要导出集合中指定字段的数据存到CVS文件中,可以使用如下命令:

>mongoexport -d localdb -c user -f _id,name --csv -o user.csv

导出的user.cvs文件的内容如下:

{ "_id" : { "$oid" : "4eed9f9ca939118694cf05e4" }, "name" : "ming" }

{ "_id" : { "$oid" : "4ef1f3cf3bd18218e6bdfa31" }, "name" : "lisi" }

4. mongoexport参数列表

options:

--help                         produce help message

-v [ --verbose ]          be more verbose (include multiple times for more verbosity e.g. -vvvvv)

--version                    print the program's version and exit

-h [ --host ] arg         mongo host to connect to ( /s1,s2 for sets)

--port arg                  server port. Can also use --host hostname:port

--ipv6                        enable IPv6 support (disabled by default)

-u [ --username ] arg     username

-p [ --password ] arg     password

--dbpath arg             directly access mongod database files in the given

path, instead of connecting to a mongod  server -

needs to lock the data directory, so cannot be used

if a mongod is currently accessing the same path

--directoryperdb        if dbpath specified, each db is in a separate directory

--journal                   enable journaling

-d [ --db ] arg           database to use

-c [ --collection ] arg   collection to use (some commands)

-f [ --fields ] arg       comma separated list of field names e.g. -f name,age

--fieldFile arg           file with fields names - 1 per line

-q [ --query ] arg     query filter, as a JSON string

--csv                        export to csv instead of json

-o [ --out ] arg         output file; if not specified, stdout is used

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

  -k [ --slaveOk ] arg (=1) use secondaries for export if available, default true

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值