Home > Software engineering >  Monogdb / pymongo 'authenticate' method call fails because no such method exists
Monogdb / pymongo 'authenticate' method call fails because no such method exists

Time:09-28

I have problem connecting to MongoDb using pymongo driver (Python 3.10, pymongo 4.2.0, MongoDb 6), specifically, authentication steps fails. Please see below my code:

import pymongo
from pymongo import MongoClient


client=MongoClient('mongodb://10.10.1.8:27017')
client.admin.authenticate('dev01','my_password_here')

I am behind company firewall, that's why you see internal IP address - 10.10.1.8

When I try to test run Python code to store data, I am getting the following error:

  File "C:\Users\ABC\Documents\python_development\pymongo_test.py", line 7, in <module>

    client.admin.authenticate('dev01','my_password_here')

  File "C:\Users\ABC\AppData\Local\Programs\Python\Python310\lib\site-packages\pymongo\collection.py", line 3194,
 in __call__ raise TypeError(
TypeError: 'Collection' object is not callable. If you meant to call the 'authenticate' method
 on a 'Database' object it is failing because no such method exists.

MongoDb sits on virtual Linux Ubuntu server that sits on top of Linux Debian server. My laptop has Windows 10.

The code looks correct, any ideas why this is happening?

CodePudding user response:

Migration guide from pymongo 3.x to 4.x https://pymongo.readthedocs.io/en/stable/migrate-to-pymongo4.html?highlight=authenticate#database-authenticate-and-database-logout-are-removed reads:

Removed pymongo.database.Database.authenticate() and pymongo.database.Database.logout(). Authenticating multiple users on the same client conflicts with support for logical sessions in MongoDB 3.6 . To authenticate as multiple users, create multiple instances of MongoClient. Code like this:

client = MongoClient()
client.admin.authenticate('user1', 'pass1')
client.admin.authenticate('user2', 'pass2')

can be changed to this:

client1 = MongoClient(username='user1', password='pass1')
client2 = MongoClient(username='user2', password='pass2')
  • Related