Export data to csv file using $in - mongodb

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

Related

mongoexport only object value

I have a really simple mongodb with only one property that i am saving, email.
I am successful at exporting that collection into a csv.
This is what it looks like.
http://imgur.com/gJKNQFH
What i would love to be able to do is only export that data this is in the 'email' property.
That way I do not have to do cleanup everything i want to use this .csv in mailchimp etc...
This is my mongoexport command
mongoexport -d myDatabase -c emails -o export.csv
Am i able to do this in mongoexport or do i need to do some custom view?
Thanks
MongoExport
Sample Document
{
"_id" : ObjectId("5694e6c5ad7b7e6a6953fb9e"),
"address" : {
"location" : "Dharmapuri",
"city" : "Chennai"
},
"author" : {
"name" : "Ponpal Johnson"
},
"book" : "origin of species"
}
MongoExport syntax
mongoexport --host <hostname> --db <Database Name> --collection <collection Name> --csv --fields fieldname1,fieldname2 --out fileName.csv
Example : Export in CSV Format
mongoexport --host localhost --db library --collection book --csv --fields address.location,author.name,book --out author.csv
Example : Export in JSON Format
mongoexport --host localhost --db library --collection book --fields address.location,author.name,book --out author.json
You should mongoexport in following way:
mongoexport -d myDatabase -c emails -f email --csv -o export.csv
where email is name of field that you want to export.
You need to give full path while exporting.

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.

Trouble with mongoexport -query command

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

Using mongoexport/mongoimport to export in csv format for embeded documents

How to properly export embeded documents in csv formats.
I would normally use this command for non-embeded documents:
mongoexport --db db_name -c collection_name --csv -f "field1,field2"
But how would you do for embeded documents. Suppose collection_name document has embeded_doc embeded in it.
You can still use the same syntax:
If you have a document with field name that is a document, such as name: { first: "kay" }, you can still do:
mongoexport --db db_name -c collection_name --csv -f "name"
If you just want to export the field first in the embedded document name
mongoexport --db db_name -c collection_name --csv -f "name.first"

Retrieving the value of _id via mongoexport

I need to export a mongo collection to CSV. I'm using mongoexport with something like:
mongoexport --db myDb --collection myCollection --fields _id,myField1,myField2 --csv --out myExport.csv
How to get the value of _id instead of ObjectID(509b82904eb8dd3621000008) for each line in the export?