您的位置:首页 > 数据库 > Mongodb

mongodb 导入导出

2019-12-05 18:38 197 查看
  • mongodump

简介:mongodump是一个实用程序,用于创建数据库内容的二进制导出。
使用场景:数据备份
注:mongodump输出只捕获数据库中的文档,不包含索引数据。
--readPreference 【从哪里读取数据,默认primary,可以指定secondary,可实现读写分离】
--viewsAsCollections【把视图作为文档导出来】
--host=<hostname><:port>, -h=<hostname><:port>
--port=<port>
--username=<username>, -u=<username>
--password=<password>, -p=<password>
--authenticationDatabase=<dbname>
--db=<database>, -d=<database>
--collection=<collection>, -c=<collection>
--query=<json>, -q=<json>【只有制定collection的情况该参数才可用】
--gzip【压缩】
--out=<path>, -o=<path>【导出的数据存放目录,同目录会覆盖原来的数据】
--archive=<file>【指定归档文件,不指定默认是stdout,跟参数--out不能同时使用】
--excludeCollection=<string>【排除哪些文档不需要导出】

例子

mongodump a Collection

> mongodump  --db=test --collection=records

mongodump a Database Excluding Specified Collections

> mongodump  --db=test --excludeCollection=users --excludeCollection=salaries

mongodump with Access Control

> mongodump --host=mongodb1.example.net --port=37017 --username=user --authenticationDatabase=admin --out=/opt/backup/mongodump-2011-10-24

Output to an Archive File

> mongodump --archive=test.20150715.archive --db=test
(mongodump --archive --db=test --port=27017 | mongorestore --archive --port=27018)

Compress the Output

> mongodump --archive=test.20150715.gz --gzip --db=test

Copy/Clone a Database

1. Use mongodump to dump the test database to an archive mongodump-test-db
> mongodump --archive="mongodump-test-db" --db=test
2. Use mongorestore with --nsFrom and --nsTo to restore (with database name change) from the archive
> mongorestore --archive="mongodump-test-db" --nsFrom='test.*' --nsTo='examples.*'
  • mongorestore

简介:mongorestore程序从mongodump创建的二进制数据库转储或标准输入(从3.0.0版开始)将数据加载到mongod或mongos实例中
使用场景:数据恢复
语法:mongorestore [options] [<directory>/<BSON file>]
注:mongorestore只执行插入,不执行更新,如果存在相同的数据,ID可能重复。

例子

Restore a Collection

> mongorestore --nsInclude=test.purchaseorders dump/
  • mongoexport

简介:mongoexport导出数据
使用场景:导出文档中的数据,避免用来备份整体数据
语法:mongoexport --collection=<coll> [options]
注:mongoexport必须指定一个文档

例子

Export in JSON Format

> mongoexport --db=sales --collection=contacts --out=contacts.json
  • mongoimport

简介:mongoimport导入数据
使用场景:导入的数据
语法:mongoimport --collection=<coll> [options]
注:

例子

Simple Import

> mongoimport --db=users --collection=contacts --file=contacts.json
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  MongoDB