Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongoimport choosing field type

Tags:

mongodb

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

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).

like image 546
Jarema Avatar asked Jun 14 '14 19:06

Jarema


People also ask

Does Mongoimport overwrite?

If you do not specify a <collection>, mongoimport imports all collections created. Existing data may be overwritten. Use this option to restore data into a MongoDB instance that already has data, or to restore only some data in the specified imported data set.

How do I use Mongoimport?

mongoimport is located in the bin directory (eg, /mongodb/bin or wherever you installed it). To import data, open a new Terminal/Command Prompt window and enter mongoimport followed by parameters such as database name, collection name, source file name, etc.

When using the Mongoimport command How can you drop the database before importing?

In case, you would like to drop any existing collection with the same name as the one you're trying to create/import, you can inform the same to mongoimport command using the –drop flag.


3 Answers

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

into

mongoimport -h foohost -d bardb -c fooc --type tsv --fields col1.int32\(\),col2.double\(\),col3.string\(\) --columnsHaveTypes --file path/to/file.txt
like image 62
finetu Avatar answered Oct 19 '22 09:10

finetu


What you can do is import these data using CSV and then run the update statement on the existing data in mongo db to convert it into the format that you want.

like image 29
Avneesh Avatar answered Oct 19 '22 11:10

Avneesh


Now version 3.4 onward mongoimport supports specifying the field types explicitly while importing the data. See below link: https://docs.mongodb.com/manual/reference/program/mongoimport/#cmdoption--columnsHaveTypes

like image 4
Ajinkya Avatar answered Oct 19 '22 11:10

Ajinkya