파이썬과 몽고DB 연동

현서·2025년 7월 16일

파이썬 웹서비스

목록 보기
4/7
post-thumbnail

1. pymongo 설치

pymongo란?
파이썬에서 MongoDB와 상호작용하기 위해 사용하는 공식 Python 클라이언트 라이브러리.
→ MongoDB에 데이터를 쉽게 삽입, 조회, 수정, 삭제할 수 있다.

!pip install pymongo

2. 몽고DB 서버와의 연결

from pymongo import MongoClient
from bson import ObjectId
url = 'mongodb_url'
client = MongoClient(url)
client
database = client['aiproject']
collection = database['user']

3. 데이터 추가

user_insert = {'userid':'apple', 'name':'김사과', 'age': 20}
result = collection.insert_one(user_insert)
print(f'입력된 데이터 id: {result.inserted_id}')
입력된 데이터 id: 6877017885a366769accf767
users_insert = [
    {'userid':'banana', 'name':'반하나', 'age': 25},
    {'userid':'orange', 'name':'오렌지', 'age': 30},
    {'userid':'melon', 'name':'이메론', 'age': 28}
]

result = collection.insert_many(users_insert)
print(f'입력된 데이터 id: {result.inserted_ids}')
입력된 데이터 id: [ObjectId('6877023685a366769accf768'), ObjectId('6877023685a366769accf769'), ObjectId('6877023685a366769accf76a')]

4. 데이터 조회하기

user_find = {'userid':'apple'}
result = collection.find_one(user_find)
print(f'데이터: {result}')
데이터: {'_id': ObjectId('6877017885a366769accf767'), 'userid': 'apple', 'name': '김사과', 'age': 20}
result = collection.find({})
for data in result:
  print(data)
{'_id': ObjectId('6877017885a366769accf767'), 'userid': 'apple', 'name': '김사과', 'age': 20}
{'_id': ObjectId('6877023685a366769accf768'), 'userid': 'banana', 'name': '반하나', 'age': 25}
{'_id': ObjectId('6877023685a366769accf769'), 'userid': 'orange', 'name': '오렌지', 'age': 30}
{'_id': ObjectId('6877023685a366769accf76a'), 'userid': 'melon', 'name': '이메론', 'age': 28}

5. 데이터 수정

user_update = {'userid':'apple'}
new_value = {'$set':{'age':30}}
result = collection.update_one(user_update, new_value)
print(result)
UpdateResult({'n': 1, 'electionId': ObjectId('7fffffff00000000000000a6'), 'opTime': {'ts': Timestamp(1752630136, 3), 't': 166}, 'nModified': 1, 'ok': 1.0, '$clusterTime': {'clusterTime': Timestamp(1752630136, 3), 'signature': {'hash': b"5\x94\xbb:$=\xde\x94\xea'\xf5\x0e,\x9f\xad\x9c!g\x96#", 'keyId': 7475774458569949188}}, 'operationTime': Timestamp(1752630136, 3), 'updatedExisting': True}, acknowledged=True)

6. 데이터 삭제

user_delete = {'userid':'apple'}
result = collection.delete_one(user_delete)
print(result)
DeleteResult({'n': 1, 'electionId': ObjectId('7fffffff00000000000000a6'), 'opTime': {'ts': Timestamp(1752630210, 5), 't': 166}, 'ok': 1.0, '$clusterTime': {'clusterTime': Timestamp(1752630210, 5), 'signature': {'hash': b'\x8e%\x9fb@}\x0c\xbe(\xd8\xb85\xc3"\x1eD\x87\xee-F', 'keyId': 7475774458569949188}}, 'operationTime': Timestamp(1752630210, 5)}, acknowledged=True)

7. 종합 예제

db = client['food_review_db']
foods = db['foods']
users = db['users']
ratings = db['ratings']
# 사용자 등록
def register_user():
    username = input("🍽️ 닉네임을 입력하세요: ")
    result = users.insert_one({"username": username})
    print(f"👋 환영합니다, {username}님!")
    return result.inserted_id

# 음식 추가
def add_food():
    name = input("🍜 음식 이름: ")
    category = input("🍱 음식 종류 (예: 중식, 한식, 분식 등): ")
    region = input("📍 배달 가능 지역: ")
    foods.insert_one({"name": name, "category": category, "region": region})
    print("✅ 음식이 등록되었습니다.")

# 음식 목록 보여주기
def show_foods():
    print("\n📦 현재 등록된 음식:")
    for food in foods.find():
        print(f"- ({food['_id']}) {food['name']} | 종류: {food['category']} | 지역: {food['region']}")

# 음식 삭제
def delete_food():
    show_foods()
    food_id = input("🗑️ 삭제할 음식의 ID를 입력하세요: ")
    foods.delete_one({"_id": ObjectId(food_id)})
    ratings.delete_many({"food_id": ObjectId(food_id)})
    print("🗑️ 음식과 관련된 모든 리뷰가 삭제되었습니다.")

# 음식에 평점 주기
def rate_food(user_id):
    show_foods()
    food_id = input("⭐ 평점 줄 음식의 ID 입력: ")
    score = float(input("🌟 평점 (0~10): "))
    comment = input("✍️ 한 줄 리뷰: ")
    ratings.insert_one({
        "user_id": user_id,
        "food_id": ObjectId(food_id),
        "score": score,
        "comment": comment
    })
    print("🎉 평점이 등록되었습니다.")

# 평점 수정
def update_rating(user_id):
    print("\n📝 내 리뷰 목록:")
    for r in ratings.find({"user_id": user_id}):
        food = foods.find_one({"_id": r["food_id"]})
        print(f" - ({r['_id']}) {food['name']}: {r['score']}점 | {r['comment']}")

    rating_id = input("수정할 리뷰 ID 입력: ")
    new_score = float(input("새 평점: "))
    new_comment = input("새 리뷰: ")
    ratings.update_one(
        {"_id": ObjectId(rating_id)},
        {"$set": {"score": new_score, "comment": new_comment}}
    )
    print("✅ 리뷰가 수정되었습니다.")

# 특정 음식 리뷰 보기
def show_food_reviews():
    show_foods()
    food_id = input("🔍 리뷰를 보고 싶은 음식 ID 입력: ")
    print("\n💬 해당 음식의 리뷰 목록:")
    for r in ratings.find({"food_id": ObjectId(food_id)}):
        user = users.find_one({"_id": r["user_id"]})
        print(f"- {user['username']} 님: {r['score']}점 | \"{r['comment']}\"")

# 평점 높은 음식 Top 3
def show_best_foods():
    print("\n🏆 별점이 높은 음식 Top 3:")
    pipeline = [
        {"$group": {"_id": "$food_id", "avg": {"$avg": "$score"}}},
        {"$sort": {"avg": -1}},
        {"$limit": 3}
    ]
    results = ratings.aggregate(pipeline)
    for r in results:
        food = foods.find_one({"_id": r["_id"]})
        print(f"- {food['name']} | 평균 별점: {round(r['avg'], 2)}점")

# 메인 메뉴
def main():
  print("🍔✨ 음식 평점 앱에 오신 걸 환영합니다! ✨🍜\n")
  user_id = register_user()

  while True:
        print("\n📋 메뉴:")
        print("1. 음식 목록 보기")
        print("2. 음식 등록하기")
        print("3. 음식 삭제하기")
        print("4. 음식 별점 남기기")
        print("5. 내 리뷰 수정하기")
        print("6. 특정 음식 리뷰 보기")
        print("7. 별점 높은 음식 TOP 3 보기")
        print("0. 종료하기")

        choice = input("👉 메뉴 선택: ")
        if choice == "1":
            show_foods()
        elif choice == "2":
            add_food()
        elif choice == "3":
            delete_food()
            pass
        elif choice == "4":
            rate_food(user_id)
            pass
        elif choice == "5":
            update_rating(user_id)
            pass
        elif choice == "6":
            show_food_reviews()
            pass
        elif choice == "7":
            show_best_foods()
            pass
        elif choice == "0":
            print("👋 안녕히 가세요! 맛있는 하루 되세요!")
            break
        else:
            print("❗ 유효한 선택지를 입력해주세요.")

if __name__ == "__main__":
  main()

🐘 studio 3T 에서 결과 확인

profile
The light shines in the darkness.

0개의 댓글