1
votes

I've a product feed collection of different category of products. I get these feeds as CSV file and using mongoimport I'm importing data into MongoDB.

My problem is now I want to add additional fields while using mongoimport, i.e. 25 fields are there in CSV file but I want to add two more columns with static value for both of them.

For example,

25 fields + "category":"xyz" , "version":123456

For each category there is a distinct CSV file. So 35 category, 35 CSV files, and I want 35 different category and version values for each.

Programmatically it is possible to read CSV and while importing add those additional fields. Is there a way with mongoimport?

1

1 Answers

0
votes

So Linux came to rescue. I've used following command before calling mongoimport.

tr \r -d < tyy-4io.csv | awk '{if(NR==1){print $0,",category,version"} else{print $0,",tyy-4io,123456"}}' > tyy-4io-detail.csv

tr is used to remove \r (CR) symbol from file and awk to make column entry for first header line and data for rest of the content.