MongoDB: Issue when using mongoexport with --query option - mongodb

When I try to take backup with mongoexport using the --query option to get the documents whose status is equal to A, facing the below error:
mongoexport --port 27017 --db ex --collection A --type=csv --fields _id,status --query '{"status":"A"}' -o eg.csv
error validating settings: query ''{status:A}'' is not valid JSON
Please let me know how to use --query option.

Assuming you run this from the DOS command prompt, you need to swap the single and double quotes. You need to wrap the entire query in double quotes and use single quotes inside the JSON document like this:
--query "{'status':'A'}"
I have tested this with mongoexport version 3.0.0 and 3.2.0 and it works for both versions.

Related

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.

mongoexport fields from subdocuments to csv

I am trying to export a field from a subdocument with no luck.
Here is my syntax;
mongoexport -d test -c accounts -f account_number,situses.coordinates -o coordinates.csv --type=csv
The output includes the account_number but not the coordinates field from the subdocument.
According to the docs, this is supposed to work.
The following will export the entire situses subdocument, but I only want the one field.
mongoexport -d test -c accounts -f account_number,situses -o coordinates.csv --type=csv
Am I just referencing the subdocument field wrong or something?
I'm running Mongodb 3.0.4
ADDITIONAL INFO
The following syntax worked on an earlier version of Mongodb (2.6.x ?). Notice the subdoc.0.fieldname syntax.
mongoexport -d test -c accounts -f account_number,situses.0.coordinates -o coordinates.csv --csv
It appears support for directly referencing a subdocument has been removed.
There is mistake in your syntax.
From mongo version 3.0.0, mongoexport removed the --type = csv option. Use the --type=csv option to specify CSV format for the output.
You should use :
mongoexport --db tests --collection accounts --type=csv --fields account_number,situses --out coordinates.csv
For nested fields you should use :
mongoexport --db tests --collection accounts --csv --fields 'account_number,situses.0.coordinates' --out /home/vishwas/c1.csv
EDIT for mongo 3.0 with sub documents:
You need to create separate collection with required fields from subdocuments like -
db.test.aggregate({"$unwind":"$situses"},{"$project":{"_id":0,"account_number":1,"siteUsesCo":"$situses.coordinates"}},{"$out" : "forcsv"})
If you want only one field from subdocument then use aggregation like -
db.test.aggregate({"$unwind":"$situses"},{"$limit":1},{"$project":{"_id":0,"account_number":1,"siteUsesCo":"$situses.coordinates"}},{"$out" : "forcsv"})
And then export from forcsv collection like-
mongoexport --db test --collection forcsv --csv --fields 'account_number,siteUsesCo' --out coordinates.csv
And after exporting delete collection forcsv.
And one more solution, where you can configure output in flexible way
mongo host:port/test --quiet query.js -u username -p passw0rd > accounts.csv
and query.js:
db = db.getSiblingDB('test');
db.getCollection('accounts').find({}, {account_number:1, situses:1, _id:0}).forEach(
function(item_data) { print(`${item_data.account_number},${item_data.situses[0].coordinates}`); });
It looks like this is a known bug to be fixed in 3.0.5.
See this; https://jira.mongodb.org/browse/TOOLS-657

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"})

mongoexport not working if you add query of selection

If you want to export any collections of any of your mongodb database, you can use mongoexport command. For example:
mongoexport --db dbname --collection collectionName --query '{"fields":1}' --out output.json
However, if you add any of selection criteria to the query, mongoexport command doesn't work. For example, if you run the following command:
mongoexport --db dbname --collection collectionName --query '{},{"fields":0}' --out output.json
the resultant JSON file includes data of every field, despite me excluding a filed named fields.
So why does this such strange behavior occur? And how can I fix it?
For your information, db.colName.find({},{"fields":0}) in mongoDB shell works as usual.
I'm on MongoDB 2.4.3 and OS X 10.9.
Thanks.
That's because --query argument takes only query and there is no option to pass projection. See mongoexport --query in official documentation. If you want to use a projection you have to add --fields option

Command fails in script, works in command line

I have a simple script to get an export from MongoDB:
#!/bin/sh -x
QUERY="'{ \"type\":\"listing\" }'"
mongoexport --db event --collection listing --query $QUERY --fields type,name --csv
(I'm using the -x switch for debugging purposes)
Here's the output when I run the script:
$ ./simple_query.sh
+ QUERY='{ "type":"listing" }'
+ mongoexport --db event --collection listing --query '{ "type":"listing" }' --fields type,name --csv
ERROR: too many positional options
However, if I merely copy and paste the mongoexport line to the prompt, it works! What's going on?
(Using MongoDB 2.0.0)
Are you using older version of mongodb? If so maybe it's this bug. It is fixed on 2.0.0-* versions.
Edit: Actually this might be different problem. You can fix it by removing the spaces on the query. Spaces seem to make the shell think it is actually multiple arguments:
QUERY="'{\"type\":\"listing\"}'"