我正在基于列表查询集合中的文档数。我想每次执行循环时都会建立一个新的连接。有更好的方法吗?
users = ['QrhcJmGIL8dFMhONApLltA', 'koLLYXk3inS3OGTB7I-CyA', 'gJTknzE8KYeqGwYLif42CQ', 'MbBhClrXyu8-Mk5wW5y8tg', 'hxDeqPbPwkKEFXOfK7av9A']
with open('tips_user_count.txt','w') as f:
for u in users:
tup = (str(tip_coll.find({"user_id": u }).count()) + "," + u)
f.write(tup+"\n")
print tip_coll.count_documents({})
>>> 1223094