Cannot import mongodb - 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

Related

Having trouble with Mongoimport

trying to import a csv file into my MongodbAtlas collection.
This is the command I run in terminal:
mongoimport --uri mongodb+srv://Jfreundlich01:<PASSWORD>#seir-cluster.sclij.mongodb.net/Optimizer --collection Players --type csv --file "Seed.csv"
Where Seed.csv is a file on my desktop.
When I click return it goes to a new line in my terminal but nothing happens.
After I run that line, if I type mongoimport --help or --version or any of those options, I get:
no such file or directory: <part of my password>#seir-cluster.sclij.mongodb.net/Optimizer
But if I close the terminal, open a new terminal and run mongoimport --version i get:
mongoimport version: 100.6.0
Any help/guidance would be greatly appreciated!
Thanks

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

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

What does "too many positional options" mean when doing a mongoexport?

mongoexport -h db.mysite.com -u myUser -p myPass -c myCollection
But the response I get is:
ERROR: too many positional options
What's that about?
I had this same problem. In my case, I was using mongoexport with the --query option, which expects a JSON document, such as:
mongoexport ... --query {field: 'value'} ...
I needed to surround the document with quotes:
mongoexport ... --query "{field: 'value'}" ...
I had the same problem. Found a group post somewhere which said to remove the space between the '-p' and the password, which worked for me.
Your sample command should be:
mongoexport -h db.mysite.com -u myUser -pmyPass -c myCollection
The same error I have encountered while importing a csv file.
But its just, the fact that the field list which you pass for that csv file import may have blank spaces.
Just clear the blank spaces in field list.
Its the parsing error.
I had the same issue with mongodump. After searching a bit, I found out that using the --out parameter to specify the output directory would solve this issue. The syntax for using the out parameter is
mongoexport --collection collection --out collection.json
Also in case your Mongodb instance isn't running, then you could use the --dbpath to specify the exact path to the files of your instance.
Source: http://docs.mongodb.org/manual/core/import-export/
I had the same issue with the mongoexport utility (using 2.0.2). My fix was to use the FULL parameter name (i.e. not -d, instead use --db).
Sometimes editor will screw it up (such as evernote). I fixed the issue by retyping the command in terminal.
I was also stuck in same situation and found what was causing it.
Make sure you are exporting in CSV format by adding parameter --type csv
Make sure there are no spaces in fields name,
Example: --fields _id, desc is wrong but --fields id,desc,price is good
This also works if you place the -c option first. For me, this order does work:
mongoexport -c collection -h ds111111.mlab.com:11111 -u user -p pass -d mydb
You can also leave the pass out and the server will ask you to enter the pass. This only works if the server supports SASL authentication (mlab does not for example).
for the (Error: Too many arguments)
Dont Use Space Between the Fields
try:
mongoexport --host localhost --db local --collection epfo_input --type=csv --out epfo_input.csv --fields cin,name,search_string,EstablishmentID,EstablishmentName,Address,officeName
Dont_Try:
mongoexport --host localhost --db local --collection epfo_input --type=csv --out epfo_input.csv --fields cin,name,search_string,Establishment ID,Establishment Name,Address,office Name
Had a similar issue
$too many positional arguments
$try 'mongorestore --help' for more information
Simply fix for me was to wrap the path location in quotes " "
This Failed:
mongorestore -h MY.mlab.com:MYPORT -d MYDBNAME -u ADMIN -p PASSWORD C:\Here\There\And\Back\Again
This Worked:
mongorestore -h MY.mlab.com:MYPORT -d MYDBNAME -u ADMIN -p PASSWORD "C:\Here\There\And\Back\Again"
I had the same issue with starting mongod. I used the following command:
./mongod --port 27001 --replSet abc -- dbpath /Users/seanfoley/Downloads/mongodb-osx-x86_64-3.4.3/bin/1 --logpath /Users/seanfoley/Downloads/mongodb-osx-x86_64-3.4.3/log.1 --logappend --oplogSize 5 --smallfiles --fork
The following error message appeared:
Error parsing command line: too many positional options have been specified on the command line
What fixed this issue was removing the single space between the '--' and 'dbpath'
I had the same issue while using the "mongod --dbpath" command. What I was doing looked somewhat like this:
mongod --dbpath c:/Users/HP/Desktop/Mongo_Data
where as the command syntax was supposed to be:
mongod --dbpath=c:/Users/HP/Desktop/Mongo_Data
This worked for me. Apart from this one may take a note of the command function and syntaxes using the mongod --help command.
In my case, I had to write the port separately from the server connection. This worked for me:
mongoexport --host=HOST --port=PORT --db=DB --collection=COLLECTION
--out=OUTPUT.json -u USER -p PASS
Create a json file in the same folder where you have your mongod.exe.
eg: coll.json
and open a command prompt in this folder.
type this below in CMD.
mongoexport --db databasename --collection collectionname --out
coll.json
and you will see like a progress bar very cool exporting all data.