1

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.

1
  • I'm not familiar with Mongo DB, but don't you have to commit or to save / close your connection somehow? Commented Jan 4, 2017 at 9:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.