ChatGPT解决这个技术问题 Extra ChatGPT

How to Import .bson file format on mongodb

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?


J
Jerry

It's very simple to import a .bson file:

mongorestore -d db_name -c collection_name /path/file.bson

Incase only for a single collection.Try this:

mongorestore --drop -d db_name -c collection_name /path/file.bson

For restoring the complete folder exported by mongodump:

mongorestore -d db_name /path/

Note: If you have enabled authentication use the below syntax:

mongorestore -u username --authenticationDatabase admin -d db_name -c collection_name /path/file.bson

Also useful for the bson file exported via mongoexport if you're only exporting single collections. mongoimport was giving me "invalid character" errors. Thanks!
SyntaxError: missing ; before statement @(shell):1:16
Maybe you can add that these commands are to be run from command prompt and not from mongo console. That would help new users
Adding to this answer. For those that want to remotely restore a bson file. Mongorestore -h [host] -u [user] -p [pass] -d [database] -c [collection] [bson file]
Thanks it worked for my complete folder after extrackting from tar.gz
M
Mirek Rusin

mongorestore is the tool to use to import bson files that were dumped by mongodump.

From the docs:

mongorestore takes the output from mongodump and restores it.

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

Do I need to keep the dump/dbName/collectionName.bson folder structure? I used mongodump, but now I want to import it using mongorestore on a remote Linux box.
@Kevin: I'm not sure, to be honest, but I would assume so. Use an FTP program to transfer the files along with the folder structure?
I was able to successfully import a mongo db using a non-"dump" folder name:mongorestore --collection people --db accounts myDump/accounts/people.bson
A
Anton Shutik
bsondump collection.bson > collection.json

and then

mongoimport -d <dbname> -c <collection> < collection.json

Note the warning from MongoDB's import-export documentation: mongoimport and mongoexport do not reliably preserve all rich BSON data types because JSON can only represent a subset of the types supported by BSON. As a result, data exported or imported with these tools may lose some measure of fidelity.
Yep. This messes up Chinese/Japanese characters.
L
Lalit Sharma

Just for reference if anyone is still struggling with mongorestore.

You have to run monogorestore in terminal/command prompt and not in mongo console.

$ mongorestore -d db_name /path_to_mongo_dump/

for more details you can visit official documentations

https://docs.mongodb.com/manual/reference/program/mongorestore/


p
puneet goyal

Run the following from command line and you should be in the Mongo bin directory.

mongorestore -d db_name -c collection_name path/file.bson


L
Looking Forward

You have to run this mongorestore command via cmd and not on Mongo Shell... Have a look at below command on...

Run this command on cmd (not on 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.

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).


I
InLaw

If your access remotely you can do it

for 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:

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"

p
phrogg

In mongodb 3.0 or above, we can specify database name to restore. Assuming that you are standing at the root directory that contains bson files

./
a.bson
b.metadata.bson
...

The script would be

for FILENAME in *; do mongorestore -d <db_name> -c "${FILENAME%.*}" $FILENAME; done

This was very useful! Thanks!
J
Jitendra Chandwani

mongorestore -d db_name /path/

make sure you run this query in bin folder of mongoDb

C:\Program Files\MongoDB\Server\4.2\bin -

then run this above command.


R
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.