pymongo: MongoClient or Connection

孤街醉人 提交于 2019-12-29 06:47:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!