如何在 mongodb 上导入 .bson 文件格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6770498/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Import .bson file format on mongodb
提问by binalay
I've exported the database on the server using mongodump
command and dump is stored in .bson
file. I need to import that in my local server using mongorestore
command. However it's not working. What is the correct mongorestore
command and what are the other tools to restore db
?
我已经使用mongodump
命令导出了服务器上的数据库,并且转储存储在.bson
文件中。我需要使用mongorestore
命令将它导入到我的本地服务器中。但是它不起作用。什么是正确的mongorestore
命令,其他的又是什么tools to restore db
?
回答by JERRY
It's very simple to import a .bsonfile:
导入.bson文件非常简单:
mongorestore -d db_name -c collection_name /path/file.bson
Incase only for a single collection.Try this:
Incase 仅用于单个集合。试试这个:
mongorestore --drop -d db_name -c collection_name /path/file.bson
For restoring the complete folder exported by mongodump
:
用于恢复导出的完整文件夹mongodump
:
mongorestore -d db_name /path/
回答by Cameron
mongorestore
is the tool to use to import bson files that were dumped by mongodump
.
mongorestore
是用于导入由mongodump
.
From the docs:
从文档:
mongorestore takes the output from mongodump and restores it.
mongorestore 从 mongodump 获取输出并恢复它。
Example:
例子:
# 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
回答by Anton Shutik
bsondump collection.bson > collection.json
and then
进而
mongoimport -d <dbname> -c <collection> < collection.json
回答by puneet goyal
Run the following from command line and you should be in the Mongo bin directory.
从命令行运行以下命令,您应该位于 Mongo bin 目录中。
mongorestore -d db_name -c collection_name path/file.bson
mongorestore -d db_name -c collection_name path/file.bson
回答by Looking Forward
You have to run this mongorestore command via cmd and not on Mongo Shell... Have a look at below command on...
你必须通过 cmd 而不是在 Mongo Shell 上运行这个 mongorestore 命令......看看下面的命令......
Run this command on cmd (not on Mongo shell)
在 cmd 上运行此命令(而不是在 Mongo shell 上)
>path\to\mongorestore.exe -d dbname -c collection_name path\to\same\collection.bson
Here path\to\mongorestore.exe
is path of mongorestore.exe
inside bin folder of mongodb. dbname is name of databse. collection_name
is name of collection.bson. path\to\same\collection.bson
is the path up to that collection.
这path\to\mongorestore.exe
是mongorestore.exe
mongodb的bin文件夹内的路径。dbname 是数据库的名称。collection_name
是 collection.bson 的名称。path\to\same\collection.bson
是该集合的路径。
Now from mongo shell you can verify that database is created or not (If it does not exist, database with same name will be created with collection).
现在从 mongo shell 您可以验证是否创建了数据库(如果它不存在,将使用集合创建同名数据库)。
回答by Lalit Sharma
Just for reference if anyone is still struggling with mongorestore.
仅供参考,如果有人仍在与 mongorestore 苦苦挣扎。
You have to run monogorestore in terminal/command prompt and not in mongo console.
您必须在终端/命令提示符而不是 mongo 控制台中运行 monogorestore。
$ mongorestore -d db_name /path_to_mongo_dump/
for more details you can visit official documentations
有关更多详细信息,您可以访问官方文档
https://docs.mongodb.com/manual/reference/program/mongorestore/
https://docs.mongodb.com/manual/reference/program/mongorestore/
回答by InLaw
If your access remotely you can do it
如果您远程访问,您可以做到
for bson:
对于 bson:
mongorestore --host m2.mongodb.net --port 27016 --ssl --username $user --password $password --authenticationDatabase $authdb -d test -c people "/home/${USER}/people.bson"
for bson compressed in .gz (gzip) format:
对于以 .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"
回答by Jitendra Chandwani
mongorestore -d db_name /path/
mongorestore -d db_name /path/
make sure you run this query in bin folder of mongoDb
确保您在 mongoDb 的 bin 文件夹中运行此查询
C:\Program Files\MongoDB\Server\4.2\bin -
C:\Program Files\MongoDB\Server\4.2\bin -
then run this above command.
然后运行上面的命令。
回答by Rochan
I have used this:
我用过这个:
mongorestore -d databasename -c file.bson fullpath/file.bson
1.copy the file path and file name from properties (try to put all bson files in different folder), 2.use this again and again with changing file name only.
1.从属性中复制文件路径和文件名(尝试将所有bson文件放在不同的文件夹中),2.一次又一次地使用它,仅更改文件名。