pymongo: MongoClient or Connection

后端 未结 3 1238
误落风尘
误落风尘 2020-12-17 19:32

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

MongoClient and Connection. 

What is the difference

相关标签:
3条回答
  • 2020-12-17 20:07

    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.

    0 讨论(0)
  • 2020-12-17 20:14

    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.

    0 讨论(0)
  • 2020-12-17 20:15

    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

    0 讨论(0)
提交回复
热议问题