python mongo

使用pymongo库

# coding=utf-8
import pymongo
import time
from pymongo import Connection

scope = 20000

con = pymongo.Connection('192.168.1.1', 27017)
db = con.test
posts = db.post
print "目前db中记录数:",
print posts.count()
con.drop_database('test')
print "目前db中记录数:",
print posts.count()
print "开始计时",
now = time.time()
print now
for j in range(scope):
    p = {"id": j, "tags": []}
    posts.insert(p)
print '插入20000条空记录',
print time.time() - now

print "为一个设备增加20000条记录, 开始计时",
now = time.time()
print now
post = posts.find_one({"id": 1})
print post

# 方法1 写数据库20000次,来插入这20000条记录
# 耗时 132秒
# for i in range(20000):
#     updatevalue = {"package":"aaa", "time":i}
#     posts.update({"id": post["_id"]}, {"$push":{"tags":updatevalue}})

# 方法2 在本地缓存中生成list对象,达到一定长度后更新到mongodb中
# 用时33秒
tmplist = []

for i in range(scope):
    tmplist.append({"package": "aaa", "time": i})
    if len(tmplist) > 2500:
        posts.update({"_id": post["_id"]}, {"$addToSet": {"tags": {"$each": tmplist}}})
        tmplist = []
if len(tmplist) > 0:
    posts.update({"_id": post["_id"]}, {"$addToSet": {"tags": {"$each": tmplist}}})
print posts.find_one({"id": 1})
print '插入20000条记录',
print time.time() - now
原文地址:https://www.cnblogs.com/yeyong/p/4598238.html