I am using pymongo
to insert a pandas dataframe (here it is coll
) as collection to MongoDB database using the following code:
mongo_url = "mongodb://%s:%s" % (host, port) # not disclosed
client = pm.MongoClient(mongo_url)
db = client[dbname]
db.coll.insert_many(coll.to_dict('records'))
This works fine. But whenever I am trying to perform the same operation using a user defined function, though I am not getting any Python error, but the database is not updated. Below is my function:
def testWrite(host, port, dbname, coll):
mongo_url = "mongodb://%s:%s" % (host, port) # not disclosed
client = pm.MongoClient(mongo_url)
db = client[dbname]
db.coll.insert_many(coll.to_dict('records'))
return 0
Here the insert_many()
method is not working (i.e. not updating the database) without throwing any kind of error. Please suggest something so that I can pass the dataframe
name in a function and push it to the database as a collection.