How to dump single field for Mongo 2.4? - mongodb

I'm trying to dump all values of just one field (id) in a particular collection using mongodump. I.e. the same as returned by this query:
db.my_collection.find({},{id:1})
I'm trying
mongodump -d my_database -c my_collection -q -q "{},{id:1}"
When I do this, it dumps all fields.
Another question has suggested
mongodump -d my_database -c my_collection -f id
But it looks like that flag has been removed because
ERROR: unknown option -f

AFAIK mongodump does not support projection. You can use mongoexport:
mongoexport -d my_database -c my_collection --fields id

Related

getting error while exporting mongodb using mongo cmd in windows

I am trying to export mongodb database using this command:
mongoexport -d db_name
But I am getting this error:
SyntaxError: missing ; before statement #(shell):1:15
What will be the command to export db?
You can use mongodump it's faster:
mongodump -d <database_name> -o <directory_backup>
And to restore/import that, i used (from directory_backup/dump/):
mongorestore -d <database_name> <directory_backup>

In mongodb can we download multiple collections with one command

Now I am using mongoexport command to download a collection and mongodump to download whole db data. Is it possible to download multiple collections with one command?
The command I use to download single collection is as below:
mongoexport -h $MONGODB_SERVICE_HOST -d countly -c collection_name -u $MONGODB_USER -p $MONGODB_PASSWORD -o /opt/app-root/src/filename
Try using automating the task by writing bash script like below:-
replace values accordingly.
db=<db>
collection_list="<collection1> <collection2> <collection3>"
host=127.0.0.1
port=<port>
out_prefix=/Temp
for collection in $collection_list; do
echo $collection
out_dir="${out_prefix}/${db}_${collection}/"
mkdir -p ${out_dir}
mongodump --host $host --port $port --collection $collection --db $db --out ${out_dir}
done

mongoexport with -f fields flag including all fields without listing one by one

Let's say I have a mongoexport command like this:
mongoexport -h mydb-a1.mlab.com:myport -d mydbname -c myCollection -u username -p password -o fileName.csv --csv -f _id,wayTooManyAdditionalFieldsToMakeCommaSepList
What is the command to just export your data with every available field?

Best practice mongodump backing up 250GB database

I'm tring another aproach. Full dump and the daily dump of new data using:
oid=$(mongo --quiet --eval 'ObjectId.fromDate(ISODate("2017-03-29 00:10:20"))')
mongodump -q "{_id:{$gt:$oid}}" -d dbname --collection name_data
But I'm getting:
Failed: error parsing query as json: invalid character ':' looking for beginning of object key string
The first $ needs to be escaped:
mongodump -q "{_id:{\$gt:$oid}}" -d dbname --collection name_data

Mongodb export csv in sorted order

I'm trying to export an entire MongoDB collection sorted by some of the fields. I'm led to believe that the following should work:
$ mongoexport --csv -d my_db -c my_collection -f field1.subfield,field2.subfield -o d.csv -q '{$query:{},$orderby:{"field1.subfield":1}}'
Unfortunately, this only exports one record in the collection (there are 18478 records) and the data exported is blank. Leaving the $orderby blank, like so,
$ mongoexport --csv -d my_db -c my_collection -f field1.subfield,field2.subfield -o d.csv -q '{$query:{},$orderby:{}}'
, exports the whole collection the way I want, so clearly the orderby clause is wrong. What am I doing wrong?
mongoexport utility does not expect you to sort data.