본문 바로가기
Research/Database

mongoDB_list메소드 누락 문제

by RIEM 2022. 11. 21.

문제

mongoDB 데이터 조회 시 list 메소드를 누락하여 type에러가 발생

raise TypeError(f"Object of type {type(o).__name__} is not JSON serializable")

TypeError: Object of type Cursor is not JSON serializable

print(db.bucket.find({}, {'_id':False})) # <pymongo.cursor.Cursor object at 0x101b7ad60>

print(list(db.bucket.find({}, {'_id':False}))) # [{'num': 1, 'msg': 'Surfing in Japan', 'done': 0}]

위 코드를 보면 list가 없으면 객체로 가져오고, list 메소드를 사용하면 리스트들을 읽을 수 있게 된다.

문제 원인

mongoDB에서 find() 메소드로 데이터들을 모두 가져올 때 객체 형태로 가져오게 되는데, list 메소드로 리스트화하지 않으면 jsonify 할 때 타입에러가 발생함.

액션

DB로부터 받는 데이터의 형태가 어떤 형태인지 파악할 필요가 있다.

 

댓글