The mongoimport
utility does not have any feature for doing this kind of manipulation of your input like you want to do in itself. This is "by design" as there are other tools that can handle this for you.
Notably there is the "pipe" |
operator which is supported in both Unix variants and Windows command prompt shells to name a few. So mongoimport
itself can read from "standard input" rather than a given --file
from "piped" input from another process doing the filtering.
A simple "perl" example ( but follow the same in scripting of choice ):
perl -pe 'chomp($_); @p = split(/\t/,$_); pop(@p); $_ = join("\t",@p) . "\n";' < persons.tsv
That will "strip" the last field from your source persons.tsv
so the output is:
Name City Mobile
A Hyd 877777
B Ban 78899
Then simply "combine" the statement with the "pipe" |
in order to pass that "input" into mongoimport
:
perl -pe 'chomp($_); @p = split(/\t/,$_); pop(@p); $_ = join("\t",@p) . "\n";' < persons.tsv | \
mongoimport --db test --collection persons --type tsv --headerline --ignoreBlanks
Which hapily creates the data:
2015-07-21T09:53:40.726+1000 connected to: localhost
2015-07-21T09:53:40.741+1000 imported 2 documents
$ mongo
MongoDB shell version: 3.0.3
connecting to: test
> db.persons.find()
{ "_id" : ObjectId("55ad8a04ee3124750e1600e7"), "Name" : "A", "City" : "Hyd", "Mobile" : 877777 }
{ "_id" : ObjectId("55ad8a04ee3124750e1600e8"), "Name" : "B", "City" : "Ban", "Mobile" : 78899 }