python连接mongodb

Posted ivon168

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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)

 
































以上是关于python连接mongodb的主要内容,如果未能解决你的问题,请参考以下文章

mongodbmongodb提升

MongoDBMongoDB的一些操作命令

MongoDBMongodb——GridFS存储

mongoDBmongoDB副本集实战

MongoDBMongoDb数据结构

MongoDBmongodb设置用户访问权限