Mongo DB and inserting bson files into a database - mongodb

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

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

The usual syntax is:
mongorestore -d dbname -c collectionname dir/file.bson

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

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/

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.

1) Go to the directory where the "dump" folder is located in CMD.
2) Run the mongorestore command.

Related

Get data from a mongodbdump.tar GZ file onto a localhost db

I am looking to make a new db on my localhost from a mongodbdump file. the file looks like this:
C:\Users\ME\FILEPATH\database\somedata-mongodbdump.tar.gz
How can I use mongo shell commands to "restore" this data to a new db on my local host? I know how to set up a new DB and insert records no problem, but I am not sure how to unload this data into one.
I am not finding anything in my searches or the documentation as to how to straightforwardly do this.
I'd like to get this file into a DB that I can host locally and use as the back end for testing an app.
I was misguided here and the previous answers is incorrect. While it may be pretty beginner stuff for some people, I will answer my own question in case anyone else runs into this:
A .tar.gz file is effectively a double zipped file. For Mongo, we need a .bson file. Unzip the file with 7zip or through CMD.
Once you have the BSON in a directory, in CMD (not mongo shell) type:
mongorestore --db 'yourdbname' --collection 'yourcollectionname' C\filepath\filepath\filename.bson
use mongo restore
mongorestore -u 'user' -p 'password -d 'database' -c 'collection' 'path to file'
if you are using a different database for auth then add
--authenticationDatabase='db for auth'

Cannot import example dataset (the system cannot find the specified file)

I am following the example given on MongoDB's website here, but I am running into trouble when trying to import sample data.
When running the command
mongoimport --db test --collection restaurants --drop --file primer-dataset.json
I get the error:
Failed: open primer-dataset.json: The system cannot find the file specified
The problem is, I am not sure what directory MongoDB expects this file to be in. I tried placing it in data/db, but that did not work. Note that I am only using default settings.
I know this is a somewhat trivial question and I feel stupid for asking it but I can not find documentation on this anywhere. Where is MongoDB expecting import files?
MongoDB expects the file to be in the directory from where you are running the command mongoimport.
If you place your file under data/db then set mongodb path as global environment variable and execute the command from data/db directory.
Additionally if you have security enabled for your mongodb then you need to execute command as below
mongoimport --username admin --password password --db test --collection restaurants --drop --file primer-dataset.json
here admin is the user authorized to perform db operations for test database and restaurants is the collection name.
For Windows!
Save file using notepad++ in .json format at MongoDB/bin where mongoimport command is present.
Simple notepad has trouble doing it.
It happened to me as well. The issue was that though the file was visible as restaurants.json actually the file was restaurants.json.json (since saved in JSON format). The issue was resolved after properly changing the name.
i have trouble like you, check you path to file, mongoimport.exe and your file may be stay in another folders.
use mongoimport -d test1 -c restaraunts companies.json for import to mongodb.
Check the filename extension, and make sure it's a ".json" file;
After this I successfully run the
mongoimport --db test --collection restaurants --drop --file [path\to\Json file]
command;
In my case, I removed the --drop parameter and it worked perfectly. I guess, it is throwing this error:
Failed: open paht/file-name.json: The system cannot find the file specified.
because the collection it wants to drop is not available, because I have not created any before.
you must copy your json file into C:\Windows\System32 and write this command on cmd:
mongoimport --db test --collection mongotest --type json --file yournamefile.json

How do I batch insert in MongoDB?

I have 40,000 lines that I wish to insert into a mongo database but I'm not sure the best way to do this.
Would I use the command line 'mongo' and paste it (I've been messing with this idea and it doesn't seem like it is ideal)
Are there any optimal or best practices here? Should I just do it in mongoid instead of mongo?
Ugh. I posted an answer here and then realized there is a command called mongoimport that can directly write the DB files.
Lets assume you have your data in JSON format, one object per line, like so:
{"a":"a1"}
{"a":"a2"}
{"a":"a3"}
You would then do:
mongoimport --dbpath PATH_TO_YOUR_DB_DIR -d DB_NAME -c COLLECTION_NAME --file JSON_FILE_NAME
The use of dbpath needs to lock the data directory, so it cannot be used if a mongod is currently accessing the same path. mongoimport --help gives you pretty useful info.

How do I dump an entire MongoDB database as text/json?

How can I dump an entire MongoDB database as text (plain text, json, or CSV)?
I'm using an application I'm not too familiar with. I'd like to
clear the database
load seed data
dump the whole db as text
do some stuff
dump again
then diff the two!
Using mongodump and bsondump:
Step 1
Dump the entire database to BSON files:
mongodump --db db1
Step 2
Convert each BSON file to JSON file:
for f in dump/db1/*.bson; do bsondump "$f" > "$f.json"; done
Hope it's helps!
You can use mongoexport utility. It will dump out json by default but you can specify to dump out csv format.
mongoexport --help will give you all the options you will need.
You mentioned that you would like to dump all the collections from a database, and mongoexport expects you to specify a database and collection name, since it expects to dump one collection.
You can write a short shell script (if you are on Windows, convert to batch script) to the effect of:
#!/bin/sh
# assuming mongo bin is in your path
host=YOURMONGOHOST
port=YOURMONGOPORT
db=DBYOUWANTTOEXPORT
for c in `mongo --quiet $host:$port/$db --eval 'db.getCollectionNames()' | sed 's/,/ /g'`
do
mongoexport --host $host --port $port -d $db -c $c > $c.json
done

How to use the dumped data by mongodump?

I have used mongodump to dump my database of mongodb, it created some bson files under dump/mydb
But I don't know how to use them. I tried mongoimport, but seems it can't import bson data. Then how to use these bson files? How to import them to another mongodb?
You need to use mongorestore, not mongoimport ... which is used for things like importing json, or csv, etc.
From the back-up-with-mongodump docs:
mongodump reads data from a MongoDB database and creates high fidelity BSON files which the mongorestore tool can use to populate a MongoDB database.
mongodump and mongorestore are simple and efficient tools for backing
up and restoring small MongoDB deployments, but are not ideal for
capturing backups of larger systems.
You can read more about mongorestore in the docs below; I'd take a look and read up on them as they are very helpful.
http://www.mongodb.org/display/DOCS/Import+Export+Tools#ImportExportTools-mongorestore
You can also check out http://learnmongo.com for tips and help!
I am using mongodump, mongorestore for daily backups and restoring from backup. I have two .bat files:
First, for backup, where you need just specify host database name and backup folder:
SET host=localhost:27020
SET dbNameToDump=MyDB
SET backupsFolder=Backups
mongodump.exe --host %host% --db %dbNameToDump%
SET date="%date:~10,4%-%date:~4,2%-%date:~7,2%.%time:~0,2%-%time:~3,2%"
cd %backupsFolder%
md %date%
xcopy /e ..\dump %date%
rmdir /s /q ..\dump
Above bat file create folder with name like this 2011-03-31.11-17(yyyy-MM-dd.hh-ss) in folder Backups with dumped collections from specified database. In files explorer it looks like so:
Second bat file i use for retore specified dumped files(here you also need specify database name and folder with dumped files):
SET host=localhost:27020
SET dbNameToRestore=MyDB
SET restoreFolder=Restore
mongorestore.exe --host %host% --db %dbNameToRestore% %restoreFolder%
In files explorer:
In additional, i am using windows schedule to automate backup process.
Hope above information will be useful for someone.
As mentioned in the previous answers, you have to use mongorestore instead of mongoimport. Adding to the previous answers, when your mongodb is running, execute the following command to restore your dump from the dump directory,
mongorestore dump
This will import all the collections into your mydb database. However this doesn't drop the database before restoring. If you wish to drop the database before importing,
mongorestore --drop dump
The bson files in the mydb directory will be restored as the collections inside mydb database. For more info on mongorestore check the documentation here.
Use mongorestore. mongoimport works on the output of mongoexport. mongodump & mongorestore work on binary data files while import / export work on json, csv, etc.. (human readable formats)
For resolving this, I copied the dump folder,dbdump(which contains bson files) to bin directory of mongodb and executed the below commands in command prompt:
1.
cd "path to MongoDB's bin folder"
(Example: cd C:\Program Files\MongoDB\Server\3.2\bin)
2.
mongorestore.exe --dir ./directory name --db database-name
(Example: mongorestore --dir ./dbdump --db testdb)
All bson files in the dump folder will be imported into your database.
You can verfiy this by executing the below commands :
cd "path to MongoDB's bin folder"
mongo.exe
show dbs;
For mongo version 3 and above use the command below:
mongorestore --host=localhost --port=27017 --username=root --authenticationDatabase=admin --db=test dump_folder/
Mongo will ask password after that