1. 좋아요 수를 기준으로 내림차순 정렬한 데이터를 가져오는 경우
https://www.w3schools.com/python/python_mongodb_sort.asp
Python MongoDB Sort
Python MongoDB Sort Sort the Result Use the sort() method to sort the result in ascending or descending order. The sort() method takes one parameter for "fieldname" and one parameter for "direction" (ascending is the default direction). Example Sort the re
www.w3schools.com
movie_star = list(db.mystar.find({}, {'_id': False}).sort('like', -1))
2. 좋아하는 영화배우를 선택하면 좋아요+1 카운트
name_receive = request.form['name_give'] # 클라이언트에서 넘어온 name값을 가지고
target_star = db.mystar.find_one({'name':name_receive}) # mystar 콜렉션에서 object를 조회
current_like = target_star['like'] # object에서 like값만 추출
new_like = current_like+1 # 좋아요+1 해줌
db.mystar.update_one({'name': name_receive}, {'$set': {'like': new_like}}) # +1된 값을 업데이트
return jsonify({'msg': '좋아요를 눌렀어요!'}) # 클라이언트에 전달
'Database' 카테고리의 다른 글
[Oracle] 실행 계획(Execution plan)과 hint사용의 필요성 (0) | 2021.07.20 |
---|---|
[Mysql] Workbench를 사용해 ERD작성 (0) | 2021.07.05 |
[pymongo] 웹스크래핑 결과를 mongoDB에 저장하기 (0) | 2021.06.27 |
[pymongo] 파이썬으로 MongoDB 사용하기 (0) | 2021.06.27 |
[NoSQL] MongoDB 설치하기 및 환경변수 설정(Window) (0) | 2021.06.26 |
댓글