sovon
sovon

Reputation: 907

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?

Upvotes: 9

Views: 3084

Answers (2)

Jason Norwood-Young
Jason Norwood-Young

Reputation: 493

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.

Upvotes: 1

myfoxtail
myfoxtail

Reputation: 14

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.

Upvotes: 0

Related Questions