Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Q 클래스
- json
- 빈생성안됨
- JPA주의사항
- jwt메서드
- json gson 차이
- JPA
- 스프링 부트 공식 문서
- REST란
- Error creating bean with name
- queryDSL
- 최종 프로젝트
- ERD 작성
- @IdClass
- 복합키
- Filter
- Spring Spring boot 차이
- spring서버
- REST API 규칙
- uncheck Exception
- 스프링 부트 기능
- jpa회원가입
- git
- github
- Unsatisfied dependency
- 스프링부트오류
- 인텔리제이
- 1차캐시
- jpa에러
- JoinColumn
Archives
- Today
- Total
Everyday Dev System
0519_TIL : 파이썬 MongoDB 연동 본문
# 문제점 :
각 팀원들의 DB를 참조하기 위해서 table명을 변수로 받아 해당 변수의 테이블에 접근하는데에 어려움을 겪었습니다.
# 시도 :
createCollection() 메서드도 사용해보았지만, 이미 존재한다고 에러가 떠서 해결되지 않았다.
두번째로 getCollection() 메서드로 해보니 깔끔하게 해결됐다!!!
# 해결방법 :
db.members.insert_one(doc)
예를 들어 members라는 테이블에 접근하기 위한 상단의 코드를
tableName = "members"
db.tableName.insert_one(doc)
이와 같은 코드로는 실행이 되지 않습니다.
이유는 tableName이라는 변수는 현재 String 타입이고, 테이블명이 들어갈 자리에는 collection이기 때문입니다.
tableName = "members"
db.get_collection(tableName).insert_one(doc)
이와 같이 변경하여 해결 하였습니다.
db.get_collection()을 이용하여 db를 참조할 수 있음을 새롭게 알게되었습니다.
ProjectCode의 일부 발췌
# 방명록 데이터 DB에 저장
@app.route("/api/comment-save", methods=["POST"])
def comment_post():
#commentId_receive = request.form['commentId_give']
commentName_receive = request.form['commentName_give']
commentContent_receive = request.form['commentContent_give']
commentPassword_receive = request.form['commentPassword_give']
islandId = request.form['islandId_give']
date = str(datetime.now().strftime('%Y-%m-%d %H:%M'))
doc = {
#'commentId':commentId_receive,
'commentName' : commentName_receive,
'commentContent' : commentContent_receive,
'commentPassword' : commentPassword_receive,
'commentDate' : date}
print(islandId)
# 본인 table명으로 변경해주세요
tableStr = islandId + "_post"
db.get_collection(tableStr).insert_one(doc)
return jsonify({'msg':'아일랜드에 발자취를 남겼다!'})
'내배캠 주요 학습 > TIL : Today I Learned' 카테고리의 다른 글
0523_TIL : Java 3주차 강의에서 새로 알게된 것 (0) | 2023.05.23 |
---|---|
0523_TIL : Java 2주차 강의에서 새로 알게된 것 (0) | 2023.05.23 |
0522_TIL : nextFloat() 문자열 입력 오류 해결 (0) | 2023.05.23 |
0523_TIL : 강민철 튜터님의 특강 : TIL 작성법 (0) | 2023.05.23 |
0510_TIL : mongoDB 연결 오류 (0) | 2023.05.10 |