8

I was given a data dump of bson files. In the mongo db, the database and the collections exists. These are updates to each of the collections in the database. So, in the given directory, there are about 30 bson files for each collection.

From the command line, I am using ubuntu, how do I append and load? Mongo is on my localhost with no username or password.

Thanks

1
  • AFAIK .bson files are created by mongodump which is what it is ... a dump of the whole db. So what do you exactly mean by 'update' to each collection? Aren't all the collections in the dump 'the updated ones' which need to replace the collections in your present mongo instance? Aug 5, 2012 at 15:59

6 Answers 6

5

Took me a while to get around this excuse for an error. In the end I went to the directory outside of my dump folder, and did the following...

For a full DB restore:

mongorestore --drop dump/mydb

Note that the mongodump operation will create individual folders for each database within the dump folder it creates, so you need to specify the full relative path, as above.

For a single collection:

mongorestore --drop -d mydb -c mycollection dump/mydb/mycollection.bson

3

The usual syntax is:

mongorestore -d dbname -c collectionname dir/file.bson
2

are you looking for mongorestore? http://www.mongodb.org/display/DOCS/Import+Export+Tools#ImportExportTools-mongorestore

2
  • I tried that. This is the error I get. mongorestore -d mydir connected to: 127.0.0.1 don't know what to do with file [dump]
    – Tampa
    Aug 5, 2012 at 16:02
  • 1
    -d is to be used for the database to restore to, why are you passing a directory to it? and you need to pass other parameters that are needed too eg the username, password, collection etc Aug 8, 2012 at 6:47
1
  • import Bson

    • mongorestore -d dbname -c collectionname dir/file.bson
  • import Json

    • mongoimport --collection NAME --file NAME.

http://docs.mongodb.org/v2.2/reference/mongoimport/

0

Since Mongo restore does not update the current records this would not be a good choice.

Mongorestore only appends new records as stated:

mongorestore just does inserts with the data to restore; if existing data (like with the same _id) is there it will not be replaced.

You may wish to build a BSON parser in your language of choice and make a more complex tool than mongorestore, since mongorestore is only designed to "restore" (as the name kinda suggests) a database/collection you will need to write something a little more complicated to do what you want and that depends heavily on your server-side language.

Edit

This is actually better done with mongoexport and mongoimport:

http://www.mongodb.org/display/DOCS/Import+Export+Tools#ImportExportTools-mongoimport

With mongoexport you could export a JSON file and give the command line for it do upserts. So I would personally go back to the person who gave this file and tell them that you actually want a mongo export file instead.

0

1) Go to the directory where the "dump" folder is located in CMD.

2) Run the mongorestore command.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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