pymongo : to check if we have connected to MongoDB database
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Beneath the City Looping
--
Chapters
00:00 Question
01:39 Accepted answer (Score 1)
02:01 Thank you
--
Full question
https://stackoverflow.com/questions/4322...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #django #mongodb #pymongo
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Beneath the City Looping
--
Chapters
00:00 Question
01:39 Accepted answer (Score 1)
02:01 Thank you
--
Full question
https://stackoverflow.com/questions/4322...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #django #mongodb #pymongo
#avk47
ACCEPTED ANSWER
Score 1
You will probably have to use the authenticate method anyway inside connection function, so you could do it as following:
In [12]: client = MongoClient(DB_MACHINE, DB_PORT)
In [13]: db = client.db_name
In [16]: if db.authenticate(DB_USER, DB_PASS, source=DB_SOURCE):
# authenticated, do ...
else:
# not authenticated, not connected, do something else