mongoexport not working if you add query of selection - mongodb

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

Related

Syntax error:missing ; before statement #(shell):1:15 [duplicate]

I'm working with mongodb 2.4.3 and I cannot get mongoexport to work. The error message I get with each command is:
"JavaScript execution failed: SyntaxError: Unexpected identifier"
At first I thought the problem was with my query parameters, which were long. But find() worked fine with the query so I know that the syntax is ok. I then created a new collection using the query and tried exporting the collection with the following:
mongoexport --db Oct2012 --collection sept8subset --csv --fields "text","created_at","user.screen_name","user.followers_count" --out sept8.csv
mongoexport --db Oct2012 --collection sept8subset --csv --fields text,created_at,user.screen_name,user.followers_count --out sept8.csv
mongoexport -d Oct2012 -c sept8subset --csv --fields text,created_at,user.screen_name -o sept8.csv
mongoexport --db Oct2012 --collection sept8subset --dbpath ~/db (should need dbpath as mongod instance is running)
mongoexport --db OCt2012 -collection sept8subset -o sept8.txt
mongoexport --db Oct2012 --collection sept8subset
In each case, I get "JavaScript execution failed: SyntaxError: Unexpected identifier". Where could that SyntaxError be??
The collection I'm trying to copy has 50,339 objects. In case it is just too big for mongoexport to handle, I took 5 documents out of the collection to make a new collection. I then tried to export them using same command structure as above. I still get the same error message.
Now I'm wondering if the problem is that mongoexport can't work with data involving dates. MongoDB documentation states that I may want to write my own export script using a client driver.
Does anyone have an idea of what my problem is here? Many thanks if you can help out.
I found out that running mongoexport from the mongo client--as I was doing--is incorrect. Mongoexport is executed at the command prompt.
I ran into this too and I solved it executing the command from the path/bin where 'mongoexport' is located.In my case :
my_path: /usr/local/Cellar/mongodb/2.4.4-x86_64/bin
"mongoexport -d book -c shelf"
'mongod' is located one step upper from 'mongoimport' , 'mongoexport', etc...

MongoDB: Issue when using mongoexport with --query option

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.

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

how to run mongoexport csv with a query

I'm trying to export a mongodb query to csv file. here's what I have:
mongoexport --db db_name --collection agents --query ‘{ $and: [ {clients_count: {$gt:2}}, {vendors_count:{$gt:10}} ] }’ --csv --fieldFile userFields.txt --out outputFilePathAndName.csv
I got the following error:
Error parsing command line: too many positional options
What am I doing wrong?
got it. correct query:
mongoexport --db db_name --collection collectionName --query '{$and:[{clients_count:{$gt:2}},{vendors_count:{$gt:10}}]}' --csv --fieldFile userFields.txt --out filepat/fileName.csv
the key is to use single quotes ' and leave no spaces in the query.
EDIT
having issue with the fields, but seems to be grabbing the correct documents.

mongoexport syntax error message

I'm working with mongodb 2.4.3 and I cannot get mongoexport to work. The error message I get with each command is:
"JavaScript execution failed: SyntaxError: Unexpected identifier"
At first I thought the problem was with my query parameters, which were long. But find() worked fine with the query so I know that the syntax is ok. I then created a new collection using the query and tried exporting the collection with the following:
mongoexport --db Oct2012 --collection sept8subset --csv --fields "text","created_at","user.screen_name","user.followers_count" --out sept8.csv
mongoexport --db Oct2012 --collection sept8subset --csv --fields text,created_at,user.screen_name,user.followers_count --out sept8.csv
mongoexport -d Oct2012 -c sept8subset --csv --fields text,created_at,user.screen_name -o sept8.csv
mongoexport --db Oct2012 --collection sept8subset --dbpath ~/db (should need dbpath as mongod instance is running)
mongoexport --db OCt2012 -collection sept8subset -o sept8.txt
mongoexport --db Oct2012 --collection sept8subset
In each case, I get "JavaScript execution failed: SyntaxError: Unexpected identifier". Where could that SyntaxError be??
The collection I'm trying to copy has 50,339 objects. In case it is just too big for mongoexport to handle, I took 5 documents out of the collection to make a new collection. I then tried to export them using same command structure as above. I still get the same error message.
Now I'm wondering if the problem is that mongoexport can't work with data involving dates. MongoDB documentation states that I may want to write my own export script using a client driver.
Does anyone have an idea of what my problem is here? Many thanks if you can help out.
I found out that running mongoexport from the mongo client--as I was doing--is incorrect. Mongoexport is executed at the command prompt.
I ran into this too and I solved it executing the command from the path/bin where 'mongoexport' is located.In my case :
my_path: /usr/local/Cellar/mongodb/2.4.4-x86_64/bin
"mongoexport -d book -c shelf"
'mongod' is located one step upper from 'mongoimport' , 'mongoexport', etc...