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

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

Related

How to mongoimport from any location in cmd

So right now I'm using mongoose to work with MongoDB (learning). Something I tried to do was import a JSON array by using
mongoimport --db mongo-testing --collection test --drop --file data.json --jsonArray
but it didn't work giving an error that mongoimport was not found. After looking around in StackOverflow, I found that you needed to run the command in the \bin\ folder where your mongo PATH variable points to. So I did just that.
mongoimport --db mongo-testing --collection test --drop --file /d/Node/mongo-test/data.json --jsonArray
The command above I ran in the \bin\ folder and pointed the file via an absolute path and it worked! However, I don't think that's the right way to do it.
Question: Is there a way I could run the command in /d/Node/mongo-test/ file where my Node.js project is or do I need to go to the bin folder each time
bin folder for mongo
UPDATE: So I tried downloading the zip file instead of the msi. After downloading it, copy it into mongoDB path in ProgramFiles (beside Server is where i put it).
C:\Program Files\MongoDB\mongodb-database-tools-windows-x86_64-100.2.0\bin
Add the above line into ur PATH variables (if you put it in the same spot).
Close all terminals and if you are using VSC terminal, close the program and restart it.
This worked for me! Hope it helps someone too

learning mongodb and importing json file but where do I save the json file?

I'm trying to learn MongoDB for the first time in a Windows 10 environment.
I found tons of posts on the syntax to import a JSON file.
My question is where does the JSON file reside?
In my Windows 10 directory, MongoDB is installed in: C:\Program Files\MongoDB\Server\3.2\bin.
I also have a data directory: C:\data\db.
I copied a sample JSON file but I don't know where to put this file. No tutorial mentions where these files should reside?
I think your question is not clear. However if your target is to import in mongodb a json file, you have to use the following command from your prompt:
C:\>mongoimport --db <db_name> --collection <collection_name> --file C:\path\to\your\json\file.json
Obviously the previous command works if you have already set mongodb environment variable. Otherwise you have to run the command from C:\path\to\mongoDB\bin.
If your target is to export a json file the command is:
C:\>mongoexport --db <db_name> --collection <collection_name> --out C:\path\to\your\output file\file.json
When one imports a json file it is converted in BSON and all your datas (documents) are saved in C:\data\db(in your case). So it's not important where is your json file; after your import what you need is in C:\data\db.

Mongodb monogorestore "root directory must be a dump of a single database"

I'm trying to restore a mongodump to a differently named database (which should be possible via --db <dbname> switch).
My working directory contains a single dump folder, which contains a single db dump.
However, when I try this command:
mongorestore --port xxxxx --db some_destination_db
I get the following error:
ERROR: ERROR: root directory must be a dump of a single database
ERROR: when specifying a db name with --db
I have no idea why I'm getting this, and can't find any help on google. Anyone have any ideas?
You need to include the path of the source db dump directory to restore from (as at MongoDB 2.6).
For example, if restoring from "twitter" to "some_destination_db" your command line would be similar to:
mongorestore --port xxxxx --db some_destination_db dump/twitter
May be your destination path contains some subfolders

Mongoexport is not working for collection

I am trying to export a Collection like this:
C:\Program Files\ConEmu>mongoexport --db test --collection person --out personTest.json
connected to: 127.0.0.1
couldn't open [personTest.json]
It is not working. Any suggestions?
You most likely are doing this from a non-elevated command prompt, and are denied to write in Program Files.
You can:
Supply a full path to a directory where you can write, --out C:\Temp\personTest.json
cd to a directory where you can write and call the executable by full path: C:\Program Files\ConEmu\mongoexport ...
Execute your command from an elevated prompt.
You do not have write permission in this directory.
Change your current location to your home directory or some other directory where you have write permission.e.g My Documents, or Desktop , or user/Andre
You need to do the following things
1.Goto C:\Program Files\MongoDB\Server\3.2\bin or it should be any path other than mongo shell.
2. If you are in this C:\Program Files\MongoDB\Server\3.2\bin path and trying to export collection then you need to run this query mongoexport --db test --collection special --out E:\spec.json . After --out you need to mention path where the exported file needs to store. If you doesn't provide this it will try to store in current directory. In some systems , drive c has root permissions so at this time it will not allow you to store file.For that you need to give paths of other drive's other than this C drive.
3.If you are using other paths it is your wish to set the path for exporting file.You can use mongoexport --db test --collection special --out E:\spec.json. or mongoexport --db test --collection special --out spec.json.
4.Check into the destination path for exported file.
Hope it helps.

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
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.