Maytree23
Maytree23

Reputation: 135

how to set the data type of mongoexport

The problem is that, I find mongoexport cannot preserve the data type in db. For example, there is a field named "tweetID", it should be a string of figures, like "23465478". After export a collection into a csv file, I found that for some entries the tweetID are exported as decimal type, like "254323467.0", while some entries are not. To avoid unnecessary mistakes, I just want to export all the fields in pure string type. Anyone knows how to set this in command mongoexport? Thanks in advance.

Upvotes: 3

Views: 2542

Answers (2)

Happy
Happy

Reputation: 21

With MongoDB Extended JSON (v2) we can preserve most of the datatypes except in certain specific cases. https://www.mongodb.com/docs/manual/reference/mongodb-extended-json/

mongoexport -u <user> -p <password> --authenticationDatabase admin --db <db-host> --collection <collection> --query '{}' --out filename.json --jsonFormat=canonical --pretty --jsonArray

Upvotes: 0

wdberkeley
wdberkeley

Reputation: 11671

You can't. If mongoexport exported 123 as 123.0, then 123 was a Double type in the document. You should try inserting the value as a 32- or 64-bit integer

db.collection.insert({ "tweetId" : NumberLong(1234567) })

mongoexport exports JSON, using strict mode JSON representation, which inserts some type information into the JSON so MongoDB JSON parsers (like mongoimport) can reproduce the correct BSON data types while the exported JSON still conforms to the JSON standard

{ "tweetId" : { "$numberLong" : "1234567" } }

To preserve all the type information, use mongodump/mongorestore instead. To export all field values as strings, you'll need to write your own script with a driver that fetches each doc and stringifies all the values.

Upvotes: 4

Related Questions