ledy
ledy

Reputation: 1617

mongoimport: append source field

With mongoimport I import the data of several external instances.

Does mongoimport allow me to add a field like source:"where-the-data-comes-from" to each document which is imported?

I.e. if i import the data of server A and B, I would like to store source:"A" or source:"B" to each document.

Upvotes: 0

Views: 733

Answers (1)

deafgoat
deafgoat

Reputation: 51

No. However, you can do this from the command line. Create a file 'header.txt' containing, e.g., (you can create this from your existing csv) by running

cat <(head -1 test.csv | tr "," "\n") <(echo source-a) > header.txt

header.txt should look like this:
field_a
field_b
.......
source

*note I have appended a 'source' field to this document. Now you can run the command (assuming you have sed installed)

sed 's/$/,source-a/' test.csv | mongoimport -d test-db -c test-cl --type csv --fieldFile header.txt

If you already have a header line in your document, run

sed '1d' test.csv | sed 's/$/,source-a/' | mongoimport -d test -c test --type csv --fieldFile header.txt instead - where 'source-a' is the label you want with this document. You can easily script this in bash so that you only supply the source and csv for each import job.

Upvotes: 4

Related Questions