Trying to restore data using bson after performing a dump

Hi There -

I used this code to dump data in a bson file from sample_supplies database. Here is the code for it -

mongodump --uri=“mongodb+srv://m001-student:m001-mongodb-basics@sandbox.mlgeq.mongodb.net/sample_supplies” --collection=sales --out=sales.bson

After running the above code in the terminal, it created a folder called sales.bson in the local host user and inside the folder there was another folder called sample_supplies and inside sample_supplies folder there are two files sales/bson and sales.metadata.json

Now, I want to restore the collection to the database using this code from the local host user -

mongorestore --uri=“mongodb+srv://m001-student:m001-mongodb-basics@sandbox.mlgeq.mongodb.net/sample_supplies” --drop dump

And it tells me this -

The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude={DATABASE}.{COLLECTION}
2021-04-24T19:12:28.658-0400 Failed: mongorestore target ‘dump’ invalid: stat dump: no such file or directory
2021-04-24T19:12:28.658-0400 0 document(s) restored successfully. 0 document(s) failed to restore.

Please help, as I want to learn how to properly dump and restore the data in the sales collection as part of the sample_supplies database. Thank you.

Regards,
Paul

Hello, @Paul_Sujith_Rayi

  • mongodump by default creates a directory with dbs and collections named “dump”
  • specified command option –out changes that default directory.

Since the dump directory was changed to “sales.bson”, we need to specify the name of directory with exported files and that is “sales.bson”:
mongorestore --uri=“mongodb+srv://m001-student:m001-mongodb-basics@sandbox.mlgeq.mongodb.net/sample_supplies” --drop sales.bson

  • in mongodump –collection option is for exporting chosen collection. For that we need to specify the name of a database - and it’s specified at the end of uri string: “sample_supplies”. Therefore in the sales.bson (–out directory) there is a folder called sample_supplies (database) and inside is one collection: sales (exported in BSON, and its metadata).

Regards,
Paweł

1 Like

Hi Pawel,

Thank you so much for this. I have tried to use mongorestore again based on what you said and this time before doing that I did not specify a name of the directory using the out command. So it created a directory called dump in the local user directory of my laptop. Inside the dump folder, I have sample_supplies directory which is the database name and inside that I have sales.bson file along with the metadata file. Now I am trying to restore into the database, the sales data that I just dumped using the mongorestore command from my command line -

mongorestore --uri=“mongodb+srv://m001-student:m001-mongodb-basics@sandbox.mlgeq.mongodb.net/sample_supplies” --drop dump

After I run the above command, my terminal tells me this -

The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude={DATABASE}.{COLLECTION}
2021-04-26T13:02:39.431-0400 building a list of collections to restore from dump dir
2021-04-26T13:02:39.431-0400 don’t know what to do with file “dump/.DS_Store”, skipping…
2021-04-26T13:02:39.431-0400 don’t know what to do with subdirectory “dump/sample_supplies”, skipping…
2021-04-26T13:02:39.432-0400 0 document(s) restored successfully. 0 document(s) failed to restore.

Hence, now I am a little confused as to what I am doing incorrectly.

Regards,
Paul

Hi Paul,

2021-04-26T13:02:39.431-0400 don’t know what to do with file “dump/.DS_Store”

  • it seems that your local OS has created metafile on its own - it’s not a proper collection file, therefore it’ll be skipped during the process.

2021-04-26T13:02:39.431-0400 don’t know what to do with subdirectory “dump/sample_supplies”, skipping…

  • probably the dump folder is inside another dump folder. If it’s possible, try to change your current working directory before running the command -> cd dump and rerun.
  • or change the optional folder to:
    mongorestore --uri=“mongodb+srv://m001-student:m001-mongodb-basics@sandbox.mlgeq.mongodb.net/sample_supplies” --drop dump/dump

Regards,
Paweł

1 Like

Hi @Pawel_Kuklinski,

I have tried again using dump/dump as you said. This time it is throwing some other error.

Can anyone form the curriculum department help me here. Thanks.

Regards,
Paul

Hi @Paul_Sujith_Rayi, I tried to reproduce your issue on my machine and it went well, as mentioned in the below stack trace, you can see that I haven’t got any error while performing the same commands as yours and it got executed successfully.

demo mongodump --uri “mongodb+srv://m001-student:m001-mongodb-basics@sandbox.mlgeq.mongodb.net/sample_supplies” --collection=sales --out=sales.bson
2021-04-27T12:42:06.728+0530 writing sample_supplies.sales to sales.bson/sample_supplies/sales.bson
2021-04-27T12:42:09.273+0530 […] sample_supplies.sales 101/5000 (2.0%)
2021-04-27T12:42:12.274+0530 […] sample_supplies.sales 101/5000 (2.0%)
2021-04-27T12:42:15.273+0530 […] sample_supplies.sales 101/5000 (2.0%)
2021-04-27T12:42:16.840+0530 [########################] sample_supplies.sales 5000/5000 (100.0%)
2021-04-27T12:42:16.841+0530 done dumping sample_supplies.sales (5000 documents)
demo ls
demo.js sales.bson
demo mongorestore --uri “mongodb+srv://m001-student:m001-mongodb-basics@sandbox.mlgeq.mongodb.net/sample_supplies” --drop sales.bson
2021-04-27T12:43:09.063+0530 preparing collections to restore from
2021-04-27T12:43:09.662+0530 reading metadata for sample_supplies.sales from sales.bson/sample_supplies/sales.metadata.json
2021-04-27T12:43:10.085+0530 restoring sample_supplies.sales from sales.bson/sample_supplies/sales.bson
2021-04-27T12:43:11.448+0530 […] sample_supplies.sales 0B/4.13MB (0.0%)
2021-04-27T12:43:14.449+0530 [########################] sample_supplies.sales 4.13MB/4.13MB (100.0%)
2021-04-27T12:43:14.785+0530 [########################] sample_supplies.sales 4.13MB/4.13MB (100.0%)
2021-04-27T12:43:14.785+0530 no indexes to restore
2021-04-27T12:43:14.785+0530 finished restoring sample_supplies.sales (5000 documents, 0 failures)
2021-04-27T12:43:14.785+0530 5000 document(s) restored successfully. 0 document(s) failed to restore.

I would recommend you to create a fresh new directory and perform the mongodump & mongorestore and please paste the whole stack trace here, so it will help me in better understanding the issue. Also don’t use a . in between the folder name, it is usually not considered as a best practice.

In case you have any doubts please feel free to reach out to us.

Thanks. Regards.
Sourabh Bagrecha,
Curriculum Services Engineer.

Hi @SourabhBagrecha,

Thank you for writing back and addressing my concern. I have tried to dump and restore this time to a different directory on my mac by navigating to it. Here is the terminal code -

Pauls-MBP:mongodb paulsujithrayi$ mongordump “mongodb+srv://m001-student:m001-mongodb-basics@sandbox.mlgeq.mongodb.net/sample_supplies” --out=/Users/paulsujithrayi/Documents/MongoDB
-bash: mongordump: command not found
Pauls-MBP:mongodb paulsujithrayi$ mongodump “mongodb+srv://m001-student:m001-mongodb-basics@sandbox.mlgeq.mongodb.net/sample_supplies”
2021-04-27T14:32:51.917-0400 WARNING: On some systems, a password provided directly in a connection string or using --uri may be visible to system status programs such as ps that may be invoked by other users. Consider omitting the password to provide it via stdin, or using the --config option to specify a configuration file with the password.
2021-04-27T14:32:53.106-0400 writing sample_supplies.sales to dump/sample_supplies/sales.bson
2021-04-27T14:32:54.585-0400 done dumping sample_supplies.sales (5000 documents)
Pauls-MBP:mongodb paulsujithrayi$ mongorestore “mongodb+srv://m001-student:m001-mongodb-basics@sandbox.mlgeq.mongodb.net/sample_supplies” --drop dump
2021-04-27T14:33:27.903-0400 The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude={DATABASE}.{COLLECTION}
2021-04-27T14:33:27.905-0400 building a list of collections to restore from dump dir
2021-04-27T14:33:27.905-0400 don’t know what to do with subdirectory “dump/sample_supplies”, skipping…
2021-04-27T14:33:27.906-0400 0 document(s) restored successfully. 0 document(s) failed to restore.

Hence, still I am not able to perform the restore in spite of moving to a different directory and trying it there. I am not able to understand what is the issues.

Do you think this has something to do with user priveieges on my mac and not mongodb. But I am the sole user of my laptop and I have root user access. So I am not able to understand what might be the issue.

Thanks & Regards,
Paul

Hi @Paul_Sujith_Rayi, it seems like you have a subdirectory in “dump” which is again named as “dump”, can you try restoring again after cd dump into the dump directory. Please also paste the output of the whole process.

Thanks. Regards.
Sourabh Bagrecha,
Curriculum Services Engineer

Hi @SourabhBagrecha,

Thank you for all your help. I was able to successfully perform mongorestore this time. And I did it from another directory rather than the local user one as you advised. Here is the entire process from start to finish.

Last login: Thu Apr 29 09:04:07 on ttys000

The default interactive shell is now zsh.
To update your account to use zsh, please run chsh -s /bin/zsh.
For more details, please visit https://support.apple.com/kb/HT208050.
Pauls-MBP:~ paulsujithrayi$ brew start serviecs mongodb-community@4.4
Error: Unknown command: start
Pauls-MBP:~ paulsujithrayi$ brew services start mongodb-community@4.4
Service mongodb-community already started, use brew services restart mongodb-community to restart.
Pauls-MBP:~ paulsujithrayi$ cd documents
Pauls-MBP:documents paulsujithrayi$ cd mongodb
Pauls-MBP:mongodb paulsujithrayi$ mongodump --uri=“mongodb+srv://m001-student:m001-mongodb-basics@sandbox.mlgeq.mongodb.net/sample_supplies” --out=/Users/paulsujithrayi/Documents/MongoDB
2021-04-30T14:02:34.932-0400 writing sample_supplies.sales to /Users/paulsujithrayi/Documents/MongoDB/sample_supplies/sales.bson
2021-04-30T14:02:36.421-0400 done dumping sample_supplies.sales (5000 documents)
Pauls-MBP:mongodb paulsujithrayi$ ls
MangoDBBasics sample_supplies
Pauls-MBP:mongodb paulsujithrayi$ cd sample_supplies
Pauls-MBP:sample_supplies paulsujithrayi$ ls
sales.bson sales.metadata.json
Pauls-MBP:sample_supplies paulsujithrayi$ cd …/…/
Pauls-MBP:documents paulsujithrayi$ cd mongodb
Pauls-MBP:mongodb paulsujithrayi$ mongorestore --uri=“mongodb+srv://m001-student:m001-mongodb-basics@sandbox.mlgeq.mongodb.net/sample_supplies” --drop dump
2021-04-30T14:07:12.488-0400 The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude=${DATABASE}.${COLLECTION}
2021-04-30T14:07:12.490-0400 Failed: mongorestore target ‘dump’ invalid: stat dump: no such file or directory
2021-04-30T14:07:12.490-0400 0 document(s) restored successfully. 0 document(s) failed to restore.
Pauls-MBP:mongodb paulsujithrayi$ mongorestore --uri=“mongodb+srv://m001-student:m001-mongodb-basics@sandbox.mlgeq.mongodb.net/sample_supplies” --drop sample_supplies
2021-04-30T14:07:25.842-0400 The --db and --collection flags are deprecated for this use-case; please use --nsInclude instead, i.e. with --nsInclude=${DATABASE}.${COLLECTION}
2021-04-30T14:07:25.844-0400 building a list of collections to restore from sample_supplies dir
2021-04-30T14:07:25.925-0400 reading metadata for sample_supplies.sales from sample_supplies/sales.metadata.json
2021-04-30T14:07:25.978-0400 restoring sample_supplies.sales from sample_supplies/sales.bson
2021-04-30T14:07:28.779-0400 [####…] sample_supplies.sales 854KB/4.13MB (20.2%)
2021-04-30T14:07:31.774-0400 [#########…] sample_supplies.sales 1.65MB/4.13MB (40.1%)
2021-04-30T14:07:34.774-0400 [##############…] sample_supplies.sales 2.49MB/4.13MB (60.3%)
2021-04-30T14:07:37.774-0400 [###################…] sample_supplies.sales 3.32MB/4.13MB (80.3%)
2021-04-30T14:07:40.774-0400 [########################] sample_supplies.sales 4.13MB/4.13MB (100.0%)
2021-04-30T14:07:41.892-0400 [########################] sample_supplies.sales 4.13MB/4.13MB (100.0%)
2021-04-30T14:07:41.892-0400 no indexes to restore
2021-04-30T14:07:41.892-0400 finished restoring sample_supplies.sales (5000 documents, 0 failures)
2021-04-30T14:07:41.892-0400 5000 document(s) restored successfully. 0 document(s) failed to restore.
Pauls-MBP:mongodb paulsujithrayi$

Thanks again for your patience and help.

Regards,
Paul

Same issue. My sales.bson file was stored in the directory C:\Users\MyUser\dump\sample_supplies\sales.bson
Solved adding /sample_supplies after the --drop dump.

Code:
<deleted - reason: potential answer>