pymongo: MongoClient or Connection

后端 未结 3 1241
误落风尘
误落风尘 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: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

提交回复
热议问题