-
Notifications
You must be signed in to change notification settings - Fork 1
/
crud.py
38 lines (29 loc) · 1012 Bytes
/
crud.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
from database import get_database
from models import Item
#CRUD (Create, Read, Update, Delete) operations for MongoDB
def add_item(item: Item):
db = get_database()
db['items'].insert_one(item.model_dump())
def update_item(item_id: str, item: Item):
db = get_database()
db['items'].update_one({'_id': item_id}, {'$set': item.model_dump()})
def delete_item(item_id: str):
db = get_database()
db['items'].delete_one({'_id': item_id})
def get_all_items():
db = get_database()
return db['items'].find({})
def search_items(query: str):
db = get_database()
items = db['items'].find({"name": {"$regex": query}})
return list(items)
def get_item_by_id(item_id: str):
db = get_database()
return db['items'].find_one({'_id': item_id})
#movies
def search_movies(title: str):
db = get_database()
movies = db['movies'].find({"title":{"$regex": title, "$options": "i"}})
return list(movies)
if __name__ == '__main__':
print(search_movies('Inception'))