I'm trying to insert/update documents in MongoDB based on information that I have in a CSV. Where if the first header of the CSV customer_id doesn't exist then it should create a new document but, if it does exist then it should just update all the values in the document.
I have the script built that will look for the customer_id and if it doesn't exist then it will create the new document but, having trouble getting the update part working.
Do you have to specify each header that needs to be updated or is there a more efficient way of updating by utilizing the headers from the CSV in the event new headers are added later on so that the script wouldn't have to be updated to specify the new headers:
import csv
from pymongo import MongoClient
conn = MongoClient('localhost', 27017)
db = conn.shipping
collection = db.sales
file = csv.reader(open("shipping_list.csv"), delimiter=',')
header = ["customer_id", "customer_name", "sales_rep", "purchase_date", "region", "purchase_price", "shipping_status", "products_purchased"]
for each in file:
if collection.count_documents({ 'customer_id': each[0] }) == 0:
row={}
for n in range(0,len(header)):
row[header[n]] = each[n]
collection.insert_one(row)
else:
row={}
for n in range(0,len(header)):
row[header[n]] = each[n]
collection.update({'customer_id': each[0]}, row)
mongoimport -d shipping -c sales --upsert --upsertFields customer_id --file shipping_list.csv--mode=upsert --headerline --type=csvAnd you may tune date format ofpurchase_datewith option--columnsHaveTypes. Have a look at the example at the bottom of the documentation page.