importing JSON file to MongoDB using command line (Robo 3T) - mongodb

I want to import a JSON file to MongoDB collection so for that i google it and it show me the followed syntax:
mongoimport --db <Database Name> --collection <Collection Name> --file <File Path>
and here is the link. I copied the same thing and typed on robo shell it through exception. can anybody let me know what am i doing wrong?
Thanks in advance

To import data into MongoDB use windows command line (cmd) as mongoimport is not mongoDB command.
NOTE: Make sure that mongoDB paths are added in environmental variables
Check out the screenshot!

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

Problems with MongoDB csv upload

I'm trying to import a csv file into MongoDB and running into "Unexpected Identifier" error.
mongoimport –-db projectexperiment -—collection structured_1000 --type csv --file /dataset/structured/structured_1000.csv --headerline
Am quite new to MongoDB and was wondering if some one could point me in the right direction
You need to run mongoimport from the terminal. Assuming you are using Windows, you need to start a DOS prompt (cmd) go to the bin directory of your mongodb installation and run your monogimport command:
C:\mongodb\bin>mongoimport –-db projectexperiment -—collection structured_1000 --type csv --file /dataset/structured/structured_1000.csv --headerline

Import a data base file.json into robo3T (robomongo)

I have a file named services.json containing a data base that I exported from a windows mongodb, and I want to import that file into robomongo (connected to mongodb installed by npm) on Ubuntu.
I'm a beginner and I don't know how to proceed, which terminal use (robomongo or Ubuntu)?
to import data for a collection in Robomongo:
Right click on collection.
Select 'insert Document'.
Paste your json data
Click validate.
Click save.
Ok, I found the answer. In shell Mac OS X or Unix type:
$ mongoimport -d your Database Name -c your Collection Name --file /path/to/my/fileThatIwantToImport.json
For anyone wishing to use mongoimport with a remote db (#andi-giga), here's what I did to make it work:
mongoimport -h xxx.mlab.com --port 2700 -d db_name -c collection_name -u user_name -p password --type json --file /Path/to/file.json
Arguments should be self-explanatory.
-h hostname
More information at this link
I don't have enough points to comment on Varun's answer, but if you use export jsonArray and then import using Robo3T (Robomongo), make sure to remove the commas in between the objects, as well as remove the square brackets.
It's not really a JSON format that ROBO 3T accepts, but rather bunch of JSON objects separated by newlines.
(if you use export Standard, then it's already formatted for document insert)
if this is not a bson, and only json, you can use mongoimport --jsonArray . refference Insert json file into mongodb
RoboMongo is just the UI for your mongod which is the primary daemon process for the MongoDB system.
The only option to import from RoboMongo is
Right Click on Collection -> Insert Document
Apart from this you can import using the mongoimport command from terminal.
Open terminal and type mongo
Now in mongo interactive shell
Use the following command to import the json file as collection
mongoimport -d database_name -c collection_name --file < path to the
json file
Tested :
mongoimport --jsonArray -d <DataBase Name> -c <Collection Name> --file /path/to/my/fileThatIwantToImport.json
It works very well!
Insert Document will insert all the JSON file data under a single document.
Apparently the tool does not support JSON import.
There are two ways to import the database into MongoDB. one is with robomongo/Robo 3T and one is with a shell command. I always choose the second method due to fewer and easy steps.
FIRST METHOD
Install MongoDB on your machine. Also, check it was installed properly or not by using mongod command on your terminal. So, for importing a new DB on your MongoDB write this command on your terminal
mongostore -host <HostIp | 127.0.0.1> -port <mongoPort | 27017> -db <DBname> <Directory-path>
So, for example you’re running MongoDB on local machine with default port i.e 27017 and your DB files are store at /usr/library/userDatabase then write this command and check DB is imported in your MongoDB
mongostore -host 127.0.0.1 -port 27017 -db userDatabase /usr/library/userDatabase
For more details check this article.
Import MongoDB using shell and robomongo/Robo 3T

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