I have a .tsv file as follows,
Name City Mobile Country
A Hyd 877777 IN
B Ban 78899 IN
Now, i don't want all the fields to be stored. I need some specific fields. I want import only Name, City and Mobile fields to Mongo DB using mongoimport. I used the below command, but its not working
mongoimport --db test --collection persons --type tsv --file persons.tsv --fields Name,City,Mobile
Final document stored in Mongo DB as follows:
{
"_id" : ObjectId("55accf948c59222984066646"),
"Name" : "A",
"Ciry" : "Hyd",
"Mobile" : "87777"
}
Could you please help me to solve this?
This is not possible as using mongoimport
you can only import whole file containing data in the database and not specific contents of a file.
To import your tsv file into the database as given above you can use:
mongoimport --db test --collection persons --type tsv --file persons.tsv --headerline
Explaination
--headerline
If using --type csv or --type tsv, uses the first line as field names. Otherwise, mongoimport will import the first line as a distinct document.
If you attempt to include --headerline when importing JSON data, mongoimport will return an error. --headerline is only for csv or tsv imports.
If your tsv file only contains the data to be imported and not the field names as the header , you can use the fields
property in mongoimport
example : mongoimport --db test --collection persons --type tsv --file persons.tsv --fields Name,City,Mobile,Country
Explaination:
--fields <field1[,field2]>, -f <field1[,field2]>
Specify a comma separated list of field names when importing csv or tsv files that do not have field names in the first (i.e. header) line of the file.
If you attempt to include --fields when importing JSON data, mongoimport will return an error. --fields is only for csv or tsv imports.