Mongoimport field type selection

When importing data from a file (csv in my case) mongoimport automatically selects the data type for each field.

Can I manually select a data type for a specific field? I came across a situation where in my file there are phone numbers that I want and which I should consider as strings, but mongoimport (quite rightly) treats these phone numbers as numbers (NumberLong).

+8
source share
4 answers

When importing CSV / TSV into mongodb, the option --columnsHaveTypes can help determine the types of columns. But the document seems very obscure. I tried several times until it finally worked out. You must add the --columnsHaveTypes option and change each column after --fields and remember to use the "\" before the "(" and ")". for example, change:

mongoimport -h foohost -d bardb -c fooc --type tsv --fields col1,col2,col3 --file path/to/file.txt 

in

 mongoimport -h foohost -d bardb -c fooc --type tsv --fields col1.int32\(\),col2.double\(\),col3.string\(\) --columnsHaveTypes --file path/to/file.txt 
+8
source

What you can do is import this data using CSV and then run the update instruction for existing data in mongo db to convert it to the desired format.

+5
source

See the Type Fidelity section in the documentation:

mongoimport and mongoexport do not reliably store all BSON-rich data types, since JSON can only represent a subset of the types supported by BSON. As a result, data exported or imported using these tools may lose some measure of fidelity. See MongoDB Extended JSON for more information.

Use mongodump and mongorestore to save types.

+3
source

Version 3.4 of onward mongoimport now supports explicitly specifying field types when importing data. See link below: https://docs.mongodb.com/manual/reference/program/mongoimport/#cmdoption--columnsHaveTypes

+2
source

Source: https://habr.com/ru/post/970845/


All Articles