pymongo로 mongoDB objectId 사용하기
mongoDB는 bson 형식으로 된 objectId를 document의 기본값으로 사용하므로 먼저 해당 라이브러리를 설치해준다
from bson.objectid import ObjectId
꺼내쓸 때
commentfind = list(db.comment.find({'posting': id_receive}))
for a in commentfind:
a['_id'] = str(a['_id'])
기본으로 넣어주는 objectId까지 꺼내와서 string으로 형변환해서 쓰면 id값만 남는다
찾을 때
id_receive = '31o32j3h1iwqeqe523'
commentfind = db.comment.find_one({'_id': ObjectId(id_receive)})
if commentfind is not None and commentfind['password'] == pwd_receive:
db.comment.delete_one({'_id': ObjectId(id_receive)})
return jsonify({"msg": "댓글 삭제 완료"})
ObjectId() 메소드에 id값을 넣어 형변환 해주면 된다
넣을 때는 기본값이라 지가 알아서 드간다 신경 쓸 필요없음
따로 id filed 값을 관계된 테이블의 속성으로 저장하지 않으려면
mongodb의 outer join을 해서 사용해야 하는데 나중에 해봐야겠다