How to mongoimport from any location in cmd - mongodb

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

Related

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.

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

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 - how do I find the install path of where the data is for the mongo database?

I am trying to do a mongorestore command and I am not sure how to find the directory of where the data is.
The command is something like this:
mongorestore -v --db new_db_dump [path to the dump directory]
and I am not sure how to find where on my local computer the current dump is so I don't know what the [path to the dump directory] is supposed to be.
Any ideas for how to find it? I am on a mac.
Thanks!
By default, mongodump places its output in a sub-directory named "dump" in the current working directory. If you forgot where you were when you executed mongodump, try searching for "dump" in the finder, look at the resulting folders named "dump", and examine the contents. There will be a sub-directory inside "dump" for each of your databases.

Cannot import mongodb

I tried mongo import like this
mongoimport -d test -c foo importfile.json
mongoimport --host localhost --db local --collection lecturer --type json --file temp.json --headerline --upsert
and I've got same error message "Syntax Error: missing ; before statement (shell):1"
what's wrong with my code & how to import if my data stored in C:\Documents and Settings\User\Desktop ?? please help, thank's in advance
mongoimport is intended to run in command prompt and not in the mongo shell. Try exiting out of the shell and running the command.
One solution is:
First, in cmd, change to the directory containing mongoexport.exe file, then type your command.
C:\Program Files\MongoDB\Server\3.2\bin> .\mongoexport.exe -d foo -c bar -o output.json
mongoimport is to be run on the terminal and not inside the mongo shell. To run mongoimport in terminal, you will need to install the same. On ubuntu, you can do :
apt-get install mongo-tools
Hope this helps :)
I had the same problem and was able to figure it out after a brief struggling and googling.
1. Navigate to the bin directory in command prompt
(cd c:..\bin)
2. Run the mongoimport command but you have to specify the full path of your json file.
That solves the problem
try to use CSV is a good.
mongoimport -d mydb -c things --type csv --file locations.csv --headerline --upsert
You can convert by ms excel.
Open the "Mongo/Server/3.4/bin" folder of mongo db in another command window and try again.It Will work.
Open a new terminal or command prompt within the location of the file you want to import and it should work. It will not work on MongoDB shell