42

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?

Shamoon
  • 103
  • 4
Aarvind
  • 421
  • 1
  • 4
  • 3
  • What is the `` doing in there? – dcrosta Sep 22 '11 at 20:45
  • I have a same problem > mongoexport --host Remote --port 27017 --collection Event --db mana --fields userName --query {"_class" : "blaablaEvent","rewardStatus" : "NEW"} --out out.csv – Hamedz Jun 16 '16 at 12:30
  • In general it is parsing error (some extra space or something like that), once try to "type" the command again instead of copying it from somewhere. – Khatri Sep 12 '16 at 07:26
  • The reason behind this might be exporting in CSV format. Try exporting in json. – Sudhir Kaushik Sep 22 '18 at 17:47

14 Answers14

42

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'}" ...
Dean Langford
  • 520
  • 5
  • 5
  • 5
    Some OS's work slightly differently. The above solution worked for me on Windows, but for a colleague (Mac) he had to put the single quotes (') on the outside and double quotes (") on the inside. Win: mongoexport ... --query "{field: 'value'}" ... Mac: mongoexport ... --query '{field: "value"}' ... – Ron Tuffin Sep 10 '13 at 08:18
  • In linux not found already the correct match using subprocess it's not working if I copy paste the command it works! – c24b Jul 16 '19 at 21:32
16

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
rowanu
  • 1,683
  • 16
  • 22
  • 1
    Additionally, watch out for spaces with the -f option. Your fields shouldn't be space out between commas – Ben G Oct 21 '14 at 21:12
  • Super weird. The same version of mongodump (2.6.11) on macOS vs Ubuntu, macOS version works with a space, the Ubuntu version does not. – DanielSmedegaardBuus Nov 14 '16 at 12:05
11

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.

eeerahul
  • 1,629
  • 4
  • 27
  • 38
7

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/

B--rian
  • 5,578
  • 10
  • 38
  • 89
4

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

3

Sometimes editor will screw it up (such as evernote). I fixed the issue by retyping the command in terminal.

Mars Zhu
  • 296
  • 2
  • 13
2

I was also stuck in same situation and found what was causing it.

  1. Make sure you are exporting in CSV format by adding parameter --type csv
  2. Make sure there are no spaces in fields name, Example: --fields _id, desc is wrong but --fields id,desc,price is good
2

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

2

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
David Buck
  • 3,752
  • 35
  • 31
  • 35
0

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'

TheLittleNaruto
  • 8,325
  • 4
  • 54
  • 73
Sean
  • 1,503
  • 2
  • 10
  • 6
0

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"
0

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.

0

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

-3

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.

A. Morales
  • 123
  • 2
  • 19