icn
icn

Reputation: 17876

pymongo: MongoClient or Connection

I am trying to connect mongodb using pymongo. I see two classes to connect to mongodb.

MongoClient and Connection. 

What is the difference of these two classes?

Upvotes: 10

Views: 13233

Answers (3)

muruga
muruga

Reputation: 1073

MongoClient and Connection are similar but MongoClient was introduced (since mongodb 2.2+ onwards) to mainly support WriteConcern and other features.

Connection is depreciated, so avoid using it in future.

The first step when working with PyMongo is to create a MongoClient to the running mongod instance. Doing so is easy:

>>> from pymongo import MongoClient
>>> client = MongoClient()

The above code will connect on the default host and port. We can also specify the host and port explicitly, as follows:

>>> client = MongoClient('localhost', 27017)

Or use the MongoDB URI format:

>>> client = MongoClient('mongodb://localhost:27017/')

Reference: MongoClient Python Example

Upvotes: 6

Jorge Puente-Sarrín
Jorge Puente-Sarrín

Reputation: 1092

Connection has been deprecated. All of the official MongoDB drivers have a new behavior using safe mode on true (No fire-and-forget).

MongoClient must be used instead of Connection.

UPDATE: All new features and changes will be made on MongoClient, not on Connection.

Upvotes: 3

sean
sean

Reputation: 3985

MongoClient is the preferred method of connecting to a mongo instance. The Connection class is deprecated. But, in terms of use they are very similar.

Upvotes: 12

Related Questions