4

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
  • Welcome to the stackexchange site. what you are getting the error? – Md Haidar Ali Khan Dec 25 '17 at 6:57
  • You can check out my answer . How to restore the properly the (.bson) file in MongoDB. – Md Haidar Ali Khan Dec 25 '17 at 7:42
2

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

| improve this answer | |
0

The above answer would work but in simple you can use Mongdb Client Studio 3T to make the restore process (partial or complete)very simple

Also they have a tutorial for how to do it

| improve this answer | |
  • ,Studio 3T - The GUI for MongoDB. It's not freeware software, it's third party software. As in professional environment of MongoDB, you have to prefer shell command compare than GUI. For your better command & understanding over query language as well as some features of MongoDB. – Md Haidar Ali Khan Dec 26 '17 at 7:29
  • Its free for general purpose if you are using it for non-commercial purpose.i accept the fact that user must know the command also.. but simplifying the things is always a better solution.. like we are using mouse click instead of command line ;) also dont worry i have mentioned in the first line that your answer would work (because i have to seen it. its way too long) – Aman Dec 26 '17 at 7:31
  • ,But OP concern is about command line query error, not specific tool or else. So, your answer should be based on that. – Md Haidar Ali Khan Dec 26 '17 at 7:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.