Help Center/
GeminiDB/
GeminiDB Mongo API/
Best Practices/
Connecting to an Instance Using Python/
Complete Example
Updated on 2024-02-07 GMT+08:00
Complete Example
#!/usr/bin/python import pymongo import random import os # There will be security risks if the username and password used for authentication are directly written into code. Store the username and password in ciphertext in the configuration file or environment variables. # In this example, the username and password are stored in the environment variables. Before running this example, set environment variables EXAMPLE_USERNAME_ENV and EXAMPLE_PASSWORD_ENV as needed. username = os.getenv('EXAMPLE_USERNAME_ENV') password = os.getenv('EXAMPLE_PASSWORD_ENV') mongodbUri = 'mongodb://%s:%s@10.66.187.127:27017/admin' client = pymongo.MongoClient(mongodbUri % (username, password )) 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
For more information about PyMongo APIs, see the official document.
Parent topic: Connecting to an Instance Using Python
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
The system is busy. Please try again later.
For any further questions, feel free to contact us through the chatbot.
Chatbot