问题描述
从文件(在我的情况下为csv)导入数据时,mongoimport会自动为每个字段选择数据类型.
When importing data from file (csv in my case) mongoimport automatically choose data type for each field.
是否可以为特定字段手动选择数据类型?我遇到了这样的情况,文件中有我想要的电话号码,我应该将其视为字符串,但是mongoimport(相当正确)将这些电话号码视为数字(NumberLong).
Is it possible to choose data type manually for specific field?I encountered situation, when in my file there are phone numbers, which I want and which I should treat as strings, but mongoimport (quite properly) treat those phone numbers as a numbers (NumberLong).
推荐答案
将CSV/TSV导入mongodb时,选项-columnsHaveTypes 可以帮助定义列类型.但是该文件似乎非常不清楚.我尝试了几次,直到最终成功.您应该添加选项-columnsHaveTypes ,并更改-fields 之后的每一列,并记住在("和)"之前使用"\".例如,更改:
When importing CSV/TSV to mongodb, the option --columnsHaveTypes can help to define the columnstypes. But the document seems very unclear. I tried several times until finally did succeed.You should add option --columnsHaveTypes and change every column after --fields and remember using "\" before "(" and ")".for example, change:
mongoimport -h foohost -d bardb -c fooc --type tsv --fields col1,col2,col3 --file path/to/file.txt
进入
mongoimport -h foohost -d bardb -c fooc --type tsv --fields col1.int32\(\),col2.double\(\),col3.string\(\) --columnsHaveTypes --file path/to/file.txt
这篇关于mongoimport选择字段类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!