python连接mongodb

import random
import time
from datetime import *
import pymongo

client = pymongo.MongoClient('***.**.**.**', *****, username='*****', password='******')

db_name = 'feeds'
db = client[db_name]


collection_set02 = db['feedsDO']

collection_set03 = db['relationDO']

count = 0
result1 = []
result2 = []
result3 = []
try:

for i in range(10003110000, 20000000000): # 200 - 299

subscriber = 100000000 + i
publisher = 600000000 + i
type = 555
msg = "MongoDB是一个介于关系数据库和非关系数据库之间的产品,是非关系数据库当中功能最丰富,最像关系数据库的。它支持的数据结构非常松散,是类似json的bson格式,因此可以存储比较复杂的数据类型。Mongo最大的特点是它支持的查询语言非常强大,其语法有点类似于面向对象的查询语言,几乎可以实现类似关系数据库单表查询的绝大部分功能,而且还支持对数据建立索引"
detailType = "888"
createTime = 1538988966588


data_01 = {"createTime":createTime,"detailType":detailType,"msg":msg,"publisher":publisher,"type":type}
data_02 = {"createTime":createTime,"detailType":detailType,"msg":msg,"publisher":publisher,"subscriber":subscriber,"type":type}
data_03 = {"publisher":publisher,"subscriber":subscriber,"type":type}

result1.append(data_01)
result2.append(data_02)
result3.append(data_03)

count += 1
if(count == 10000):
collection_set02.insert_many(result1)
collection_set02.insert_many(result2)
collection_set03.insert_many(result3)

print(i)
result1 = []
result2 = []
result3 = []
count = 0
except pymongo.errors.DuplicateKeyError:
print('record exists')
except Exception as e:
print(e)

原文地址:https://www.cnblogs.com/perTest/p/9777106.html