Is there anyway to specify default values for empty cells when using mongoimport
to import a CSV file.
I import CSV data like this:
usename,password,salt
xxx@ss.com,12345,,
xxx@ss.com,asdf,,
xxx@ss.com,ads,,
but I want to set salt
to some default value like saltdefault
.
How can I achieve that with mongoimport
, or do you have some other way to realize it?
To my knowledge there is no such thing like default values for importing CSV values via mongoimport
.
But you can of course prepare the data upfront by using some script, e.g. the following python script would do the job when called with the input CSV file as argument
import csv
import sys
in_name = sys.argv[1]
out_name = in_name + '.post'
with open(in_name, newline="") as in_f, open(out_name, "w", newline="") as out_f:
reader = csv.reader(in_f)
writer = csv.writer(out_f)
# Keep heading
row = next(reader)
writer.writerow(row)
for row in reader:
if len(row[2]) == 0:
row[2] = 'Your default'
writer.writerow(row)
Another solution is to first import your data into a MongoDB collection and then run an update command against it filling the documents with empty salt
.