Skip to content Skip to sidebar Skip to footer

How To Connect Mongodb With Python?

I'm calling function with argument value database name. When I print argument other function it's working properly but when i'm connecting this argument value with database it's no

Solution 1:

You need to fetch the database directly, as its passed into your method:

from pymongo import MongoClient
client = MongoClient('localhost:27017')

def my_function(mydb):
    db = client.get_database(mydb)
    return db.collection.find().count()

print(my_function('my_database'))

Post a Comment for "How To Connect Mongodb With Python?"