tencent cloud

Feedback

Python Connection Sample

Last updated: 2024-01-15 14:49:55

    Notes

    TencentDB for MongoDB provides two usernames rwuser and mongouser by default to support the MONGODB-CR and SCRAM-SHA-1 authentication methods, respectively. The connecting URIs for the two authentication methods are formed differently. For more information, see Connecting to TencentDB for MongoDB Instance.
    Download pymongo 3.13.0. For more information, see MongoDB Python Drivers.

    Getting Started

    Python sample code 1

    #!/usr/bin/python
    import pymongo
    import random
    
    mongodbUri = 'mongodb://mongouser:thepasswordA1@10.66.187.127:27017/admin'
    
    client = pymongo.MongoClient(mongodbUri)
    db = client.somedb
    db.user.drop()
    element_num=10
    for id in range(element_num):
    name = random.choice(['R9','cat','owen','lee','J'])
    sex = random.choice(['male','female'])
    db.user.insert_one({'id':id, 'name':name, 'sex':sex})
    
    content = db.user.find()
    for i in content:
    print i
    

    Python sample code 2

    #!/usr/bin/python
    import pymongo
    mongodbUri = 'mongodb://mongouser:thepasswordA1@10.66.187.127:27017/admin'
    client = pymongo.MongoClient(mongodbUri)
    db = client.someonedb
    
    inserted_id = db.somecoll.insert_one({"somekey":"yiqihapi"}).inserted_id
    print inserted_id
    
    for doc in db.somecoll.find(dict(_id=inserted_id)):
    print doc
    
    for doc in db.somecoll.find({"somekey":"yiqihapi"}):
    print doc
    The output information is as follows:
    5734431e101e2f6d699b37ef
    {u'somekey': u'yiqihapi', u'_id': ObjectId('5734431e101e2f6d699b37ef')}
    {u'somekey': u'yiqihapi', u'_id': ObjectId('5734431e101e2f6d699b37ef')}
    
    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support