MongoDB操作

连接

1
2
3
4
5
6
7
from pymongo import MongoClient

client = MongoClient(MONGO['host'], MONGO['port'])  # 可以设置最大连接池。
monitor_db = client.monitor_db  # 连接数据库,如果不存在 就新建
self.collection = monitor_db[str(target)]  # 选择集合,如果不存在 就新建
# 创建索引,设置过期时间,特别地,该字段必须是 UTC 时间(datetime.datetime.utcnow()),否则不会过期。
self.collection.create_index([("createAt", 1)], expireAfterSeconds=3600)