MongoDB操作 发表于 2019-11-30 | 分类于 python 连接 1234567from 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) 查