def test_find_update():
db = Connection()
db.drop_collection("test")
db.test.insert({"x":1,"y":2})
start = time.time()
for i in range(1,10000):
y = db.test.find_one()
print db.test.find_one()
print time.time()-start
db.drop_collection("test")
start = time.time()
for i in range(1,10000):
db.test.insert({"x":1,"y":2})
print db.test.find_one()
print time.time()-start
db.drop_collection("test")
db.test.insert({"x":1,"y":2})
start = time.time()
for i in range(1,10000):
db.test.update({},{"$inc":{"x":1,"y":2}})
print db.test.find_one()
print time.time()-start
结果:
{u'y': 2, u'x': 1, u'_id': ObjectId('4ffd159ae3f0f8103a000000')}
**9.78821802139**
{u'y': 2, u'x': 1, u'_id': ObjectId('4ffd15a4e3f0f8103a000001')}
**0.82381606102**
{u'y': 200000, u'x': 100000, u'_id': ObjectId('4ffd15a5e3f0f8103a002710')}
**0.635884046555**
我认为 find 运算符可能很便宜,但这与我的假设相反。谁能告诉我为什么 find operator 如此耗时?