How do I bulk update/insert in mongoDb with pymongo/pandas.
The error I get is batch op errors occurred
I reason I get is because I set the "_id", which I want to do. I code runs fine on first run, but on second run it fails. I want to use pandas in workflow. The data does have a datetime object.
The syntax is completely different for upsert = True, with Update. An efficient solution with update would be helpful, where "_id" or "qid" could be set. But, there are python datetime objects!
InSQL = 'SELECT * from database2.table2 '
sqlOut = pd.read_sql(InSQL,cxn)
sqlOut['_id'] = "20170101" + ":"+ sqlOut['Var']
dfOut = sqlOut.to_json(orient='records',date_format='iso' )
try:
db["test"].insert_many(json.loads(dfOut))
except Exception as e: print e
I have given a 50pt bounty, which expired, with no answer. Hmm...
string base inputand not allow any variable inserting(operator only)check this,json.loads(dfOut)you can't insert local variable as entry ! Primary keyYYYYmmDDHHMMSS+Counter valueBulkWriteErrordetails and get a better idea of what's going on. It may be that your personally defined ids are duplicated or violate the 12-byte limit. See stackoverflow.com/questions/30355790/mongodb-bulk-write-error for details._idkeys). Can you update with a short example of thedfOutcontent and the upsert code you tried? Also, what version of PyMongo are you using? I suspect you actually want to useupdate_many()with theupsertoption set. Can you also clarify the concerns on Python datetime objects and_idversusqid? Do you want to use theqidas the_id?