mongoimport: error parsing command line options: cannot use both --file and a positional argument to set the input file - mongodb

I'm trying to upload a .json file to my db with mongoimport. i'm using this chain of commands (without the sensitive info):
mongoimport <host uri that starts with: mongodb+srv://...> --authenticationDatabase "admin" --authenticationMechanism= "SCRAM-SHA-1" --collection "..." --file "C:\Users\Jose Miguel\Documents\tinto programa\tinto-programas-3\database\DATA\JSON\2022\JUNIO\JUNIO 2022.json" --username "..." --password "...."
The complete error is the following:
error parsing command line options: error parsing positional arguments: cannot use both --file and a positional argument to set the input file
what could be wrong? i've already read the documentation and everything seems to be in its right place.
EDIT: as per #Joe suggestions, I have edited my command chain to look like this:
mongoimport <connection string, now without quotes> --authenticationMechanism "SCRAM-SHA-1" --authenticationDatabase=admin --collection=<collection_name> --file "C:\Users\Jose Miguel\Documents\tinto programa\tinto-programas-3\database\DATA\JSON\2022\JUNIO\JUNIO 2022.json" --username=<...> --password=<...>

A positional argument is one that is separated from the rest of the command by whitespace, and not introduced with a preceding option that starts with a dash.
Mongoimport can take at most 2 positional arguments: a connection string and a file
Check <host uri that starts with: mongodb+srv://...>, if there are any spaces or shell-reactive characters, escape and/or quote the string
Many of the options to mongoimport can use either = or space to separate the option from its argument, however --authenticationMechanism= "SCRAM-SHA-1" may be interpreted as an empty string for auth mechanism, with "SCRAM-SHA-1" as a positional argument. Use either --authenticationMechanism="SCRAM-SHA-1" or --authenticationMechanism "SCRAM-SHA-1"

Related

Mongo import using shell script not working

I am trying to import json using bash script but it does not seem to be working properly
The error that I am getting is:
error validating settings: incompatible options: --file and positional argument(s)
Here is the script that I am trying to run
importserver="localhost:27017"
username="username"
password="password"
importdb="Hotel"
collections=("Customers")
echo "Begin To Import"
for c in ${collections[#]}
do
echo "importing $c .."
mongoimport $importserver --db $importdb --collection $c --file "$c.json"
done
echo "Done."
I have tried changing params and everything. None seems to work
You are very close. Need to add the --host parameter...
importserver="localhost:27017"
username="username"
password="password"
importdb="Hotel"
collections=("Customers")
echo "Begin To Import"
for c in ${collections[#]}
do
echo "importing $c .."
mongoimport --host $importserver --db $importdb --collection $c --file "$c.json"
done
echo "Done."
The error message error validating settings: incompatible options: --file and positional argument(s) is saying there is a parameter - the host name parameter - that has no tag and therefore must be identifiable by its position, but positional arguments usually go at the end. Rather than use a positional argument, if you add the --host it will be a named argument instead. I think there are some gotchas about which parameters are allowed to be positional and which are allowed to be named. I think the file can be either, but I don't think host is.

Error validating settings: only one positional argument is allowed

The problem: I need to import a csv file to a mongodb, but i need to specify the field types and still use the --headline cause in my csv the first line is where the fieldnames are.
Here is what i'm tryin:
mongoimport --type csv -d solution2 -c data --headerline --drop dados.csv direction.int32\(\),latitude.double\(\),longitude.double\(\),metrictimestamp.date\(\),odometer.int32\(\),routecode.int32\(\),speed.int32\(\),device_deviceid.int32\(\),vehicle_vehicleid.int32\(\) --columnsHaveTypes
"solution2" is the DB name, "data" is the collection and "dados.csv" is my archive.
I'm getting this error message: Error validating settings: only one positional argument is allowed.

mongodump using --query argument gives "positional arguments not allowed" error

I'm using mongodb 2.6 and trying to create a dump using the query option gives "positional arguments not allowed".
I am trying to get all the products who parameter's timestamp is between specified range and whose id is of any of the specified format.
mongodump --host 10.xx.xxx.xx:xxxx --db test --collection products --username abc --password uvw --query '{"parameterList":{$elemMatch:{ "paramName":"TimeStamp","paramValue":{$gte:"20160620000000",$lt:"20160724000000"}}},"parameterList.paramValue": {$in:[/SPC126/,/CSC234/]}}' --authenticationDatabase test --out "c:\New folder\dump"
document structure
{
"_id": ObjectId("590074c362f41f15144996fa"),
"product": "device1",
"parameterList":[{"paramName":"TimeStamp",
"paramValue":"20160731000700"},
{"paramName":"Id",
"paramValue": "SPC126332"}]
}
Unlike UNIX bash, Windows cmd.exe doesn't recognize single quotes as a delimiter.
Running your example command as-is in cmd.exe gives the error:
Error parsing command line: too many positional options
Try changing your quotes around, replacing the single quotes with double quotes and vice versa. For example, using the example command you posted:
mongodump --host 10.xx.xxx.xx:xxxx --db test --collection products --username abc --password uvw --query "{'parameterList':{$elemMatch:{ 'paramName':'TimeStamp','paramValue':{$gte:'20160620000000',$lt:'20160724000000'}}},'parameterList.paramValue': {$in:[/SPC126/,/CSC234/]}}" --authenticationDatabase test --out "c:\New folder\dump"
Note the --query "..." instead of --query '...' in the example above.
It should be able to complete the dump successfully.

mongodump showing error: Error parsing command line: too many positional options

mongodump:
--host hostname:port -u User -p password--authenticationDatabase admin --dbdb_name --collection collection_name –q {db.getCollection('col_name').find({"statement.context.platform":"abc","statement.timestamp":{'$gte':'2016-03-30T00:00:00','$lte':'2016-04-20T23:59:59'}})}"
Error:
Error parsing command line: too many positional options
Mongodb version- 2.6
Spaces between arguments are not correct
https://docs.mongodb.com/manual/reference/program/mongodump/#cmdoption--query says:
You must enclose the query in single quotes (e.g. ') to ensure that it does not interact with your shell environment.
and your query doesn't have starting qoute, but it ends with double quote.
Hope that this helps.

Mongoexport error parsing query

Im trying to do a mongoexport to CSV but only selecting certain records with a query. Here's my command (windows 7 cmd):
mongoexport --host foo.com --port 27017 --username bar -p --db foo --csv --fields col1,col2,col3 --collection bar --out dump_q.csv --query '{"recent":"yes"}'
However after entering the password, I get an error:
assertion: 16619 code FailedToParse: FailedToParse: Expecting '{': offset:0
The command works fine without the query argument but I cant figure out whats wrong with the query:
--query '{"recent":"yes"}'
Any help much appreciated
Summary of answer:
Make sure you use double quotes on enclose the query and single quotes to enclose strings e.g.
--query "{'recent':'yes'}"
Also make sure you don't have a space in your query otherwise the command prompt will parse it as another argument. So don't have:
--query "{'recent': 'yes'}"
(notice the space in-between)
Queries which include nested fields don't work such as:
--query "{'folder.recent':'yes'}"
You'll need to use double quotes to contain the query string (and either single quotes or two quotes to escape inside of the string)
--query "{'recent':'yes'}"
Complete:
mongoexport --host foo.com --port 27017 --username bar -p
--db foo --csv --fields col1,col2,col3
--collection bar --out dump_q.csv --query "{'recent':'yes'}"
From mongoexport documentation:
--query , -q
Provides a JSON document as a query that optionally limits the documents returned in the export.
Your query string seems to be correctly formated. You can even ommit the double quotes around recent.
Single or double quotes don't seem to matter, as long as you are persistent in using different types on the outside and the inside.
Are you sure this is a valid query though? What is the output if you run the following in the database? What about a find()?
db.bar.count({"recent":"yes"})