0

I have a test.tsv TSV file that has a header looking like this

sample.string() organism.string()   capture.string()    sex.string()

and I am using the following command to import my file into mongodb

mongoimport --quiet -d somedb --collection=somecollection --file=test.tsv --type tsv --mode upsert --upsertFields sample --columnsHaveTypes --headerline

I wish to add the date following mongodb format at the moment of my file import.

I found a way to do it while in mongodb but I can't seem to find anything about mongoimport.

Is there a command (or a workaround) that looks like the following and allows us to add the date of the file content ?

mongoimport --quiet -d somedb --collection=somecollection --file=test.tsv --type tsv --mode upsert --upsertFields sample --columnsHaveTypes --headerline --addDate

Thanks in advance.

user324810
  • 597
  • 8
  • 20

1 Answers1

1

I found a workaround for this:

First, get the date from bash with some formatting:

date=$(date -u +"%d-%m-%Y %T")

Now, add this variable to all the lines in file.tsv like so:

awk -v date="$date" '{print $0 "\t" date}' file.tsv

Then, we can change the header by adding the date format type

$ sed -i '1s/.*/Sample.string()\tOrganism.string()\tCapture.string()\tSex.string()\tDate.date(02-01-2006 15:04:05)/' file.tsv
$ head file.tsv
Sample.string()     Organism.string()     Capture.string()     Sex.string()     Date.date(02-01-2006 15:04:05)

Then, we can import with mongoimport:

$ mongoimport -d somedb --collection=somecollection --file=file.tsv --type tsv --mode upsert --upsertFields Sample --columnsHaveTypes --headerline
2019-09-30T11:23:37.934+0200    connected to: xxxx
2019-09-30T11:23:37.940+0200    imported 1 document

mongocompass

user324810
  • 597
  • 8
  • 20