当前位置:Gxlcms > 数据库问题 > mondb08---导入导出

mondb08---导入导出

时间:2021-07-01 10:21:17 帮助过:14人阅读

Mongodb数据的导入导出 1: 导入/导出可以操作的是本地的mongodb服务器,也可以是远程的. 所以,都有如下通用选项:(本地机就不用这个了) -h host 主机 --port port 端口 -u username 用户名 -p passwd 密码 2: mongoexport 导出json格式的文件 问: 导出哪个库,哪张表,哪几列,哪几行? -d 库名 -c 表名 -f field1,field2...列名 -q 查询条件 -o 导出的文件名 -- csv 导出csv格式(便于和传统数据库交换数据) 例: [root@localhost mongodb]# ./bin/mongoexport -d test -c news -o test.json connected to: 127.0.0.1 exported 3 records [root@localhost mongodb]# ls bin dump GNU-AGPL-3.0 README test.json THIRD-PARTY-NOTICES [root@localhost mongodb]# more test.json { "_id" : { "$oid" : "51fc59c9fecc28d8316cfc03" }, "title" : "aaaa" } { "_id" : { "$oid" : "51fcaa3c5eed52c903a91837" }, "title" : "today is sataday" } { "_id" : { "$oid" : "51fcaa445eed52c903a91838" }, "title" : "ok now" } D:\mongodb\bin目录中开启git bush窗口: mongoexport -d shop -c goods -f goods_name,shop_price -q {shop_price:{$lte:1000}} -o ./shop.out.json mongoexport -d shop -c goods -f goods_name,shop_price -q {shop_price:{$lte:1000}} --csv -o ./shop.out.csv 注: _id列总是导出 Mongoimport 导入 -d 待导入的数据库 -c 待导入的表(不存在会自己创建) --type csv/json(默认) --file 备份文件路径 例1: 导入json D:\mongodb\bin目录中开启git bush窗口: $ mongoimport -d shop -c animal --file ./shop.out.json //animal表可以不存在 2017-12-30T22:58:51.545+0800 connected to: localhost 2017-12-30T22:58:52.016+0800 imported 17 documents 例2: 导入csv ./bin/mongoimport -d test -c goods --type csv -f goods_id,goods_name --file ./goodsall.csv ./bin/mongoimport -d test -c goods --type csv --headline -f goods_id,goods_name --file ./goodsall.csv //headline 是跳过第一行标题行, mongodump 导出二进制bson结构的数据及其索引信息 -d 库名 -c 表名 -f field1,field2...列名 例: mongodum -d test [-c 表名] 默认是导出到mongo下的dump目录 规律: 1:导出的文件放在以database命名的目录下 2: 每个表导出2个文件,分别是bson结构的数据文件, json的索引信息 3: 如果不声明表名, 导出所有的表 mongorestore 导入二进制文件 例: ./bin/mongorestore -d test --directoryperdb dump/test/ (mongodump时的备份目录) 二进制备份,不仅可以备份数据,还可以备份索引, 备份数据比较小. //可以写脚本,定时导出。

 

mondb08---导入导出

标签:art   god   ...   test   aaa   rest   备份文件   ima   mongodump   

人气教程排行