我已经在服务器上使用mongodb命令导出了数据库,dump存储在.bson文件中。我需要使用mongorestore命令将其导入本地服务器。然而,它并没有工作。正确的mongorestore命令是什么?还有哪些恢复db的工具?
当前回答
如果你能远程访问,你就能做到
bson:
mongorestore --host m2.mongodb.net --port 27016 --ssl --username $user --password $password --authenticationDatabase $authdb -d test -c people "/home/${USER}/people.bson"
对于压缩为。gz (gzip)格式的bson:
mongorestore --host m2.mongodb.net --port 27016 --ssl --username $user --password $password --authenticationDatabase $authdb -d test -c people --gzip --dir "/home/${USER}/people.bson.gz"
其他回答
Mongorestore是用来导入被mongodb转储的bson文件的工具。
从文档中可以看出:
Mongorestore从mongodb获取输出并恢复它。
例子:
# On the server run dump, it will create 2 files per collection
# in ./dump directory:
# ./dump/my-collection.bson
# ./dump/my-collection.metadata.json
mongodump -h 127.0.0.1 -d my-db -c my-collection
# Locally, copy this structure and run restore.
# All collections from ./dump directory are picked up.
scp user@server:~/dump/**/* ./
mongorestore -h 127.0.0.1 -d my-db
如果你能远程访问,你就能做到
bson:
mongorestore --host m2.mongodb.net --port 27016 --ssl --username $user --password $password --authenticationDatabase $authdb -d test -c people "/home/${USER}/people.bson"
对于压缩为。gz (gzip)格式的bson:
mongorestore --host m2.mongodb.net --port 27016 --ssl --username $user --password $password --authenticationDatabase $authdb -d test -c people --gzip --dir "/home/${USER}/people.bson.gz"
Mongorestore -d db_name /path/
请确保在mongoDb的bin文件夹中运行此查询
C:\Program Files\MongoDB\Server\4.2\bin -
然后执行上述命令。
bsondump collection.bson > collection.json
然后
mongoimport -d <dbname> -c <collection> < collection.json
你必须通过cmd运行这个mongorestore命令,而不是在Mongo Shell上…看看下面关于…的命令。
在cmd上运行这个命令(不是在Mongo shell上)
>path\to\mongorestore.exe -d dbname -c collection_name path\to\same\collection.bson
这里的路径\to\mongorestore.exe是mongodb bin文件夹中的mongorestore.exe路径。Dbname为数据库名称。Collection_name是collection.bson的名称。相同路径\ \ \集合。Bson是到集合的路径。
现在从mongo shell你可以验证数据库是否被创建(如果它不存在,同名的数据库将创建集合)。
推荐文章
- 如何排序mongodb与pymongo
- 如何在mongodb上导入。bson文件格式
- JSON文件的蒙古导入
- 如何删除mongodb中的数组元素?
- 修改MongoDB数据存储目录
- 在MongoDB中查找重复的记录
- 为什么MongoDB Java驱动在条件中使用随机数生成器?
- 在猫鼬,我如何排序的日期?(node . js)
- 将映像存储在MongoDB数据库中
- 重复Mongo ObjectId的可能性在两个不同的集合中生成?
- Redis比mongoDB快多少?
- 无法连接到服务器127.0.0.1:27017
- 如何创建数据库的MongoDB转储?
- 如何将MongoDB作为Windows服务运行?
- 如何监听MongoDB集合的变化?