mongodb数据的导入导出

来源:互联网 发布:数控车床编程步骤例题 编辑:程序博客网 时间:2024/05/23 11:31

导出

1.导出整个数据库

mongodump -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 -o 文件存在路径

如果想导出所有数据库,可以去掉-d 默认端口是27017

2.导出整张表

[root@localhost mongodb]# mongoexport -d test -c users -o /home/mongodb/test/users.dat connected to: 127.0.0.1 exported 24 records

3.导出表中部分字段

[root@localhost mongodb]# mongoexport -d test -c users --csv -f uid,name,sex -o test/users.csv connected to: 127.0.0.1 exported 24 records

4.根据条件敢出数据

[root@localhost mongodb]# mongoexport -d test -c users -q '{uid:{$gt:1}}' -o test/users.json connected to: 127.0.0.1 exported 12 records

导入

mongorestore -h IP --port 端口 -u 用户名 -p 密码 -d 数据库 --drop 文件存在路径

导入时如果出现如下错误:

terminate called after throwing an instance of 'std::runtime_error'   what():  locale::facet::_S_create_c_locale name not valid

在终端输入

export LC_ALL="en_US.utf8"

再执行导入操作即可