Retrieving the value of _id via mongoexport - mongodb

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?

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

How to take mongodump for 1 collection from my Database in MongoDB

How to take mongodump for 1 collection from my Database
../mongodump --db db_name --collection collection_name --out /home/dell/999/
i got this error
bash: ../mongodump: No such file or directory
This is working for entire db backup
./mongodump --out /home/dell/777/ --db dbname
But back up for single collection from a database not working
Use mongoexport to export collection data:
mongoexport --db test --collection mycollection --out myCollection.json
If it's a replica set and you want to use the --uri you should use it like this cause documentation states that you can't specify some options when using --uri
mongodump --uri "mongodb://user:password#mongo-en-1.example.io:27017,mongo-en-2.example.io:27017,mongo-en-3.example.io:27017/$Databasename?replicaSet=$replicasetname&authSource=admin" --collection $collectionname

export csv from MongoDB

I am new to MongoDB. I want to export some fields to csv file and if that field is present in particular row then I want empty value in that field. Currently I am trying this:
mongoexport --host hostname --collection collectionname -q '{}' -f "field1","field2" --db dbname --username user --password pass --out out.csv
But problem is that output does not keep the field if field value is not present in the database. Any suggestion how can I perform the desired operation?
Try:
mongoexport --host hostname --username user --password pass --db dbname --collection collectionname --type=csv --fields field1,field2 --query '{field1: { $exists: true}, field2: { $exists: true}}' --out out.csv
For more detail: Click here

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.

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"