Files
Hentai_manga_parser/HBD.py
2025-11-03 00:19:20 +03:00

42 lines
1.3 KiB
Python
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from pymongo import MongoClient
#wdawfqfqwfqwfqw
def connect_to_mongo():
"""Подключение к MongoDB"""
client = MongoClient("mongodb://localhost:27017/")
db = client["Manga"]
return db["Test"]
def find_doc_with_max_id(collection):
"""
Находит документ с максимальным значением в поле 'id'
и возвращает его 'id' и 'num'
"""
# Сортируем по убыванию и берем первый документ
doc = collection.find_one(
{"num": {"$exists": True}},
sort=[("num", -1)],
projection={"id": 1, "num": 1, "_id": 0}
)
if not doc:
return None # Если нет подходящих документов
return {
"id": doc["id"],
"num": doc.get("num") # Используем get() на случай отсутствия поля
}
if __name__ == "__main__":
collection = connect_to_mongo()
result = find_doc_with_max_id(collection)
if result:
print(f"Максимальный ID: {result['id']}")
print(f"Соответствующий num: {result['num']}")
else:
print("Документы не найдены или поле 'id' отсутствует")