MongoDB中怎么迁移部分数据

MongoDB中怎么迁移部分数据

MongoDB中怎么迁移部分数据,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

场景:将mongodb部分数据从一台云服务器迁移到另一台云服务器

命令:mongoexport、mongoimport 

$ mongoexport -d db -c collection -o outfile --type json/csv -f fields
-h, --host 远程连接的数据库地址,默认连接本地Mongo数据库。
--port 远程连接的数据库的端口,默认为27017.
-u, --username 连接远程数据库的账户,若数据库设置了认证,需指定账户。
-p, --password 连接远程数据库账户对应的密码
--authenticationDatabase 认证的数据库
-d, --db 数据库名称
-c, --collection 集合名称
-o, --out 导出的文件名
--type 导出的文件格式,默认为JSON,可选CSV、JSON。
-f, --fields 导出的字段,多字段以逗号分隔,当输出格式为CSV是必须指定输出的字段,CSV大部分关系型数据库都支持。
-q, --query 查询条件
--skip 跳过指定数量的数据
--limit 读取指定数量的数据记录
--sort 对数据进行排序,可指定排序的字段,使用1为升序-1为降序,如 sort({key:1})


# 导出集合
$ mongoexport --host 127.0.0.1 --port 27017 --username sa --password sa --authenticationDatabase game --db test --collection test --type json --out ./test.json
# 简化方式
$ mongoexport -d test -c tf_game -o ./test.json


# 导入集合
$ mongoimport --host 127.0.0.1 --port 27017 --username sa --password sa --authenticationDatabase game --db test --collection test --file ./test.json

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注行业资讯频道,感谢您对亿速云的支持。