问题
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?
回答1:
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.
回答2:
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
回答3:
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.
来源:https://stackoverflow.com/questions/13959790/pymongo-mongoclient-or-connection