Updated on 2022-09-21 GMT+08:00

Complete Example

#!/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

For more information about PyMongo APIs, see the official document.