I have below in a shell script to export certain fields from a mongo collection to a CSV file.
mongoexport --host localhost --db mydb --collection ratings --csv > data.csv --fields userId,filmId,score
My problem is that the result is generated comes with the header values.
ex:
userId,filmId,score
517,533,5
518,534,5
Is there a way that I can generate the csv file with out the header fields?
The mongoexport
utility is very spartan and does not support a load of features. Instead the intention is that you augment with other available OS commands or if you really must then create your own code for explicit needs.
But this sample using tail
is quite simple to skip the first emitted header line when you consider that all output is going to STDOUT
by default anyway:
mongoexport --host localhost --db mydb --collection ratings \
--fields userId,filmId,score \
| tail -n+2 > data.csv
So it is just "piping through" |
the tail
command with the -n+2
option, that basically says "skip the first line" and then you just redirect >
output to the file you want.
Just like most command line utilities, there is no need to build in options that can be performed with other common utilities in such a chained pattern as above. That is why there is no such option built in.