MongoDB Connection Error with Python

27 views
Skip to first unread message

Abhishek Sharma

unread,
Nov 11, 2019, 9:19:34 AM11/11/19
to Django users
class MongoConnect(object):
def __init__(self,HOST,PORT,DB,USERNAME,PASSSWORD):
self.HOST=HOST
self.PORT=PORT
self.USERNAME=USERNAME
self.PASSSWORD=PASSSWORD
self.DB=DB
def getMongoConnetion(self):
client= MongoClient(self.HOST, self.PORT, username=self.USERNAME, password=self.PASSSWORD)
return client

def getDBObject(self, DB):
connection = getMongoConnetion()
return connection[DB].authenticate(USERNAME,PASSSWORD)



MONGO_DB = 'techcraft'
instance=MongoConnect('127.0.0.1',27017,'techcraft','craft','dbcl123456')
client=instance.getMongoConnetion()
db=instance.getDBObject('techcraft')
print(db)



############Error##########

Traceback (most recent call last):
  File "self.py", line 36, in <module>
    db=instance.getDBObject('techcraft')
  File "self.py", line 28, in getDBObject
    connection = getMongoConnetion()
NameError: name 'getMongoConnetion' is not defined

Kasper Laudrup

unread,
Nov 11, 2019, 10:12:41 AM11/11/19
to django...@googlegroups.com
Hi Abhishek,

On 11/11/2019 15.19, 'Abhishek Sharma' via Django users wrote:
>
> ############Error##########
>
> Traceback (most recent call last):
>   File "self.py", line 36, in <module>
>     db=instance.getDBObject('techcraft')
>   File "self.py", line 28, in getDBObject
>     connection = getMongoConnetion()
> NameError: name 'getMongoConnetion' is not defined
>

In your getDBObject function you need an instance for your
getMongoConnetion() function, eg. self.getMongoConnetion().

Kind regards,

Kasper Laudrup
Reply all
Reply to author
Forward
0 new messages