9

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?

sovon
  • 877
  • 2
  • 12
  • 28

2 Answers2

1

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.

0

mongoexport won't do what you want because of moongoimport - these tools are used for exporting/importing a database and they use only one format. You should be trying to use mongodump or mongorestore.

myfoxtail
  • 14
  • 1
  • mongodump and mongorestore use binary format isn't it? Are you saying it is impossible to do this with mongoexport? – Raj006 Dec 05 '18 at 02:46