mongo5数据库中导出和导入数据

1、安装

 用mongoexport和mongoimport工具实现

新版mongodb中不再包含导入导出工具,需要单独下载安装,下载地址:

Download MongoDB Command Line Database Tools | MongoDBhttps://www.mongodb.com/try/download/database-tools选择对应的版本下载安装即可

curl -o mongodb_tools.tgz https://fastdl.mongodb.org/tools/db/mongodb-database-tools-rhel70-x86_64-100.5.1.tgz

 解压,移动到需要的位置

tar -xvf mongodb_tools.tgz
mv mongodb-database-tools-rhel70-x86_64-100.5.1 ../app/mongodb_tools

配置环境变量

export PATH=/home/lht/app/mongodb_tools/bin:$PATH

环境变量生效

source ~/.bashrc

测试

mongoimport --version

输出:

mongoimport version: 100.5.1
git version: d62df26a96814651d4e70c619b68a518dc6b048a
Go version: go1.16.7
   os: linux
   arch: amd64
   compiler: gc

2、导出

mongoexport -d lhtdb -c lhtab -f _id,addr,loc --type=csv -o ~/out3.csv

-d        数据库名

-c        集合(表)名

-f        导出列名,如果导出为csv文件,此为必选项

-o        导出的文件名

导出之后查看文件,内容如下:

[lht@localhost ~]$ cat out.csv 
_id,addr,loc
1,海联大厦,"{""type"":""Point"",""coordinates"":[113.733006,34.776711]}"
2,金融大厦,"{""type"":""Point"",""coordinates"":[113.733719,34.77605]}"
3,郑银大厦,"{""type"":""Point"",""coordinates"":[113.734433,34.775076]}"
4,中科大厦,"{""type"":""Point"",""coordinates"":[113.734851,34.774274]}"
5,人保大厦,"{""type"":""Point"",""coordinates"":[113.735152,34.773406]}"
6,农信大厦,"{""type"":""Point"",""coordinates"":[113.732153,34.777258]}"
7,格拉姆-B,"{""type"":""Point"",""coordinates"":[113.73108,34.777883]}"
8,宝龙广场,"{""type"":""Point"",""coordinates"":[113.734656,34.781072]}"
9,会展中心,"{""type"":""Point"",""coordinates"":[113.729918,34.773123]}"

3、导入

mongoimport -d lhtdb -c lhtimp --type=csv --headerline --file=out.csv

-d                        数据库名

-c                        集合(表)名

--type=csv          指定导入的文件格式

--headerline       指定第一行为表头,这一行不导入数据库

--file                   指定导入的文件名

查看导入的数据:

> db.lhtimp.find()
{ "_id" : 1, "addr" : "海联大厦", "loc" : "{\"type\":\"Point\",\"coordinates\":[113.733006,34.776711]}" }
{ "_id" : 2, "addr" : "金融大厦", "loc" : "{\"type\":\"Point\",\"coordinates\":[113.733719,34.77605]}" }
{ "_id" : 3, "addr" : "郑银大厦", "loc" : "{\"type\":\"Point\",\"coordinates\":[113.734433,34.775076]}" }
{ "_id" : 4, "addr" : "中科大厦", "loc" : "{\"type\":\"Point\",\"coordinates\":[113.734851,34.774274]}" }
{ "_id" : 5, "addr" : "人保大厦", "loc" : "{\"type\":\"Point\",\"coordinates\":[113.735152,34.773406]}" }
{ "_id" : 6, "addr" : "农信大厦", "loc" : "{\"type\":\"Point\",\"coordinates\":[113.732153,34.777258]}" }
{ "_id" : 7, "addr" : "格拉姆-B", "loc" : "{\"type\":\"Point\",\"coordinates\":[113.73108,34.777883]}" }
{ "_id" : 8, "addr" : "宝龙广场", "loc" : "{\"type\":\"Point\",\"coordinates\":[113.734656,34.781072]}" }
{ "_id" : 9, "addr" : "会展中心", "loc" : "{\"type\":\"Point\",\"coordinates\":[113.729918,34.773123]}" }

搞定!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值