TypeError: 'MongoClient' object is not callable

Viewed 501

I have this newbie problem in my python program which is given below , I tried my best to solve it. but I can't solve it by my own, can you guys help me here is my code.

import pymongo

client = pymongo.MongoClient('mongodb://127.0.0.1:27017/')

db = client('Employee')

information = db.employeeinfo
records = {
    "fname": "Dhruvin",
    "lname": "Prajapati",
    "role": "Developer"
}
information.insert_one(records)

2 Answers

You're trying to execute client as if it was a function. To access a database, use either the subscription or dot notation:

db = client['Employee']
# alternatively: db = client.Employee

That error occurs when you try to call, with (), an object that is not callable. In python, a callable object can be a function or a class.

To access the DB from MongoClient object you should use it like client['Employee'] or client.Employee

import pymongo

client = pymongo.MongoClient('mongodb://127.0.0.1:27017/')

db = client.Employee

information = db.employeeinfo
records = {
    "fname": "Dhruvin",
    "lname": "Prajapati",
    "role": "Developer"
}
information.insert_one(records)

please find the document link for MongoClient here https://pymongo.readthedocs.io/en/stable/tutorial.html

Related