Gerrit
Gerrit

Reputation: 2677

MongoClient is not callable, but why?

in my Flask App I've imported pymongo with: from pymongo import MongoClient

Then I call my connect_db-method:

db = get_db(connect_db())

The method coding is like:

def connect_db():
    client = MongoClient("localhost", 27017)
    return client

def get_db(client):
    return client(DATABASE)

But I get the error TypeError: 'MongoClient' object is not callable

I find it on https://api.mongodb.org/python/current/tutorial.html

What's the reason?

Upvotes: 1

Views: 4441

Answers (2)

Nick
Nick

Reputation: 1

One thing I must point out,mongoClient is not available in php7,pls be careful if you'r using this php version.

Upvotes: 0

garnertb
garnertb

Reputation: 9594

With pymongo you access a Client's database using attribute style access or dictionary style access:

def connect_db():
    client = MongoClient("localhost", 27017)
    return client

client = connect_db()
db = client.database_name
# or 
db = client['database-name']

Upvotes: 7

Related Questions