Trouble with mongoexport -query command - mongodb

I am trying to export a subset of documents that contain a specific ObjectId from a collection of Twitter searches. To do this, I am using the following script:
mongoexport --db twitter --collection tweets --csv --fieldFile CSVfields.txt --out .\tweets.csv --query query.txt
...where CSVfields.txt references the specific document keys that I want in the export and query.txt contains:
{ "search" : ObjectId("525f9cfdb3685db029000001") }
When I run this, I get an error saying:
assertion: 16619 code FailedToParse: FailedToParse: Expecting '{': offset:0
Any idea what I am doing wrong?
Thanks!

--query takes a JSON query, not a file.
So either of the following should work:
mongoexport --db twitter --collection tweets --csv --fieldFile CSVfields.txt --out .\tweets.csv --query `cat query.txt`
mongoexport --db twitter --collection tweets --csv --fieldFile CSVfields.txt --out .\tweets.csv --query '{ "search" : ObjectId("525f9cfdb3685db029000001") }'

Related

Export data to csv file using $in

I am trying to export data to csv file using mongo export with some condition with the following command
mongoexport --db <db_name> --collection <coll_name> --query "'meta.metaData.fieldName' : {$in : [EZR-2016-21123,EZR-2016-22016, EZR-2016-23420]}}" --fields <field_name> --type=csv --out out_file_name.csv
This works fine for other normal query, but when I put $in, it doesn't work.
You need to updated the query to be the following
mongoexport --db <db_name> --collection <coll_name> --query "{'meta.metaData.fieldName' : {'$in' : ['EZR-2016-21123', 'EZR-2016-22016', 'EZR-2016-23420']}}" --fields <field_name> --type=csv --out out_file_name.csv
You are missing the starting { off your query
Strings within the $in array need to be quoted

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

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