This repository has been archived on 2024-05-31. You can view files and clone it, but cannot push or open issues or pull requests.
Telegram/modules/database.py

43 lines
1.4 KiB
Python
Raw Normal View History

2023-01-03 16:12:46 +02:00
"""Module that provides all database columns and
creates geospatial index for col_applications"""
2022-12-31 00:29:17 +02:00
from pymongo import MongoClient, GEOSPHERE
2022-12-10 12:37:15 +02:00
from ujson import loads
with open("config.json", "r", encoding="utf-8") as f:
db_config = loads(f.read())["database"]
f.close()
if db_config["user"] is not None and db_config["password"] is not None:
con_string = 'mongodb://{0}:{1}@{2}:{3}/{4}'.format(
db_config["user"],
db_config["password"],
db_config["host"],
db_config["port"],
db_config["name"]
)
else:
con_string = 'mongodb://{0}:{1}/{2}'.format(
db_config["host"],
db_config["port"],
db_config["name"]
)
db_client = MongoClient(con_string)
db = db_client.get_database(name=db_config["name"])
collections = db.list_collection_names()
2022-12-14 14:57:29 +02:00
for collection in ["tmp", "users", "context", "messages", "warnings", "applications", "sponsorships"]:
2022-12-10 12:37:15 +02:00
if not collection in collections:
db.create_collection(collection)
2022-12-14 14:57:29 +02:00
col_tmp = db.get_collection("tmp")
2022-12-10 12:37:15 +02:00
col_users = db.get_collection("users")
col_context = db.get_collection("context")
col_messages = db.get_collection("messages")
col_warnings = db.get_collection("warnings")
2022-12-10 16:52:07 +02:00
col_applications = db.get_collection("applications")
2022-12-28 19:56:13 +02:00
col_sponsorships = db.get_collection("sponsorships")
col_applications.create_index([("application.3.location", GEOSPHERE)])