0

I am trying to create a connection to a database - collection using a ClientSession in pymongo 3.7.1. From the example provided by the doc here : https://docs.mongodb.com/manual/core/transactions/, it seems possible to get a connection to a database and cursor.

I tried something really similar :

import pymongo
client = pymongo.MongoClient('mongodb://localhost:27017')  
session = client.start_session()   
session.get_database('test')  

But the last line raise the following exception :

AttributeError: 'ClientSession' object has no attribute 'get_database'

I also tried two other more direct access to the database :

session['test_database']
session.test_database

which raised :

TypeError: 'ClientSession' object has no attribute 'getitem'
AttributeError: 'ClientSession' object has no attribute 'db_test'

How does the ClientSession behaves ? Apparently is does not inherit from the Client object, otherwise the previous line would work...

Does someone have an idea ?

Heemanshu Bhalla
  • 3,603
  • 1
  • 27
  • 53
Alexis Rosuel
  • 563
  • 1
  • 5
  • 12

1 Answers1

1

Edit I just found, I have to use session.client to retrieve the Client from the ClientSession

Alexis Rosuel
  • 563
  • 1
  • 5
  • 12