Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export from mongodb with converted object id to string

I want to export from mongodb where objectId will be converted to string when exporting.

ObjectId("507c7f79bcf86cd7994f6c0e").toString()

This does not work with export command. I tried the following but that showing syntax error.

./mongoexport --host localhost --db Database --collection collection_name --type=csv --out collection.csv --fields _id.toString()

How can I do this?

like image 791
sovon Avatar asked May 31 '26 11:05

sovon


1 Answers

I don't think you can do this with a single command, but after running your export, you can use sed to convert to a string.

sed -i 's/ObjectId(\([[:alnum:]]*\))/\1/g' collection.csv

I got the pattern from here.

like image 170
Jason Norwood-Young Avatar answered Jun 04 '26 04:06

Jason Norwood-Young