Tampa
Tampa

Reputation: 78244

Mongo DB and inserting bson files into a database

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

Upvotes: 8

Views: 28350

Answers (6)

Paras Mathur
Paras Mathur

Reputation: 172

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

2) Run the mongorestore command.

Upvotes: 0

Engineer
Engineer

Reputation: 8847

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

Upvotes: 5

Anderson Lopes
Anderson Lopes

Reputation: 639

  • 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/

Upvotes: 1

zag2art
zag2art

Reputation: 5172

The usual syntax is:

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

Upvotes: 3

Sammaye
Sammaye

Reputation: 43884

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.

Upvotes: 0

Related Questions