MongoDB import bson files from a folder

mongodbmongodumpmongorestore

I am unable to import (.bson) file on my computer to MongoDB database .I have been trying below commands :

1)mongorestore -d demo -c dcoll C:\data\dump\twitter\tweets.bson

and

2)mongorestore -d demo -c dcoll /dump/twitter/tweets.bson

Best Answer

mongorestore -d demo -c dcoll C:\data\dump\twitter\tweets.bson

As i have gone through your MongoDB syntax query it seems to be correct. As per your script code demo is database and dcoll is collection and you want to restore the (.bson) file of tweets.bson in your dcoll collection.

I would like to say that here before restore the (.bson) file in MongoDB. You have to make sure that your database name and collection name should be in MongoDB. If you have don't have Collection in database then first create that Collection in database.

For example first connect with mongo shell and show the database and collection . Which you are going to restore in MOngoDB, which is exist or not.

To show the database in MongoDB

For example in my environment i have checked and these databases are exist in MongoDB.

> show dbs
admin             0.000GB
blog              0.011GB
citibike          0.338GB
city              0.002GB
enron             0.213GB
local             0.000GB
ships             0.001GB
test              0.164GB
video             0.237GB
week6             0.006GB

Suppose that i want to restore (.bson) file in test database.

> use test
switched to db test
>

Then i shall create the collection data in test database.

> db.createCollection("data")
{ "ok" : 1 }   // ok : 1 means collection successfully created 

Also confirm from the query , the data collection is in test database or not.

> show collections
airline
amwaj
cars
collection
data
movies
stuff
>

Here data collection is present then finally restore the (.bson) into the data collection.

Note: Make sure that you are restoring the (.bson) file from the mongo shell where mongorestore.exe is present. Basically this file is available in BIN folder of the MongoDB Server. In my place it is C:\Program Files\MongoDB\Server\3.6\bin.

so, the finally mongorestore command will be like that

C:\Program Files\MongoDB\Server\3.6\bin>mongorestore -d test -c data C:\data\dump\100YWeatherSmall\data.bson
    2017-12-25T09:55:50.531+0300    checking for collection data in C:\data\dump\100YWeatherSmall\data.bson
    2017-12-25T09:55:50.561+0300    reading metadata for test.data from C:\data\dump\100YWeatherSmall\data.metadata.json
    2017-12-25T09:55:50.565+0300    restoring test.data from C:\data\dump\100YWeatherSmall\data.bson
    2017-12-25T09:55:52.662+0300    [##......................]  test.data  43.9MB/403MB  (10.9%)
    2017-12-25T09:55:55.565+0300    [#####...................]  test.data  87.6MB/403MB  (21.7%)
    2017-12-25T09:55:58.501+0300    [#########...............]  test.data  152MB/403MB  (37.6%)
    2017-12-25T09:56:01.501+0300    [#############...........]  test.data  230MB/403MB  (57.1%)
    2017-12-25T09:56:04.501+0300    [################........]  test.data  282MB/403MB  (70.0%)
    2017-12-25T09:56:07.501+0300    [######################..]  test.data  377MB/403MB  (93.6%)
    2017-12-25T09:56:09.349+0300    [########################]  test.data  403MB/403MB  (100.0%)
    2017-12-25T09:56:09.349+0300    no indexes to restore
    2017-12-25T09:56:09.350+0300    finished restoring test.data (250000 documents)
    2017-12-25T09:56:09.350+0300    done

Here in above code i have used

mongorestore command for restoring the (.bson) file.

-d // for database
-c // for collection

C:\data\dump\100YWeatherSmall\data.bson // location of the (.bson) file.

I hope this will help out to you.

For further your ref mongorestore