@staticmethod
def aggregate(collection, query):
return DB.DATABASE[collection].aggregate(query)
def listing():
user_id = ECTOKEN.get_user_id(object)
event_category_receive = request.args.get("event_category_give")
if event_category_receive == "":
events = DB.list('event', {}, {'_id': False})
cafes = DB.list('cafe', {}, {'_id': False})
else:
events = DB.aggregate('cafe',
{'$lookup': {'from': "event", 'localField': "idx", 'foreignField': "idx", 'as': "cafes"}},
{'$unwind': 'cafes'},
{'$project': {'idx': 1, 'cafe_name': 1, 'cafe_short_info': 1, 'event_category': '$cafes.event_category'}})
cafes = DB.list('events', {'event_category': event_category_receive}, {'_id': False})
for cafe in cafes:
cafe_idx = str(cafe['idx'])
cafe["count_heart"] = DB.count_documents('heart', {"cafe_idx": cafe_idx, "type": "heart"})
cafe["heart_by_me"] = bool(DB.find_one('heart', {"cafe_idx": cafe_idx, "type": "heart", "user_id": user_id}, {'_id': False}))
cafe["bookmark_by_me"] = bool(DB.find_one('heart', {"cafe_idx": cafe_idx, "type": "bookmark", "user_id": user_id}, {'_id': False}))
return jsonify({"result": "success", 'cafes': cafes, 'events': events, 'event_category': event_category_receive})
lookup으로 조인
파이썬으로 mongoDB 제어하기