• 카테고리

    질문 & 답변
  • 세부 분야

    풀스택

  • 해결 여부

    미해결

tot_count = board.find({}).count() 구문오류

22.01.15 04:25 작성 조회수 555

1

안녕하세요 박사님 구문오류 관련 질문이 있습니다.
 
현재 저는 몽고디비 4.0.1 버전 이용중입니다.
 
아래 코드를 실행하면
AttributeError: 'Cursor' object has no attribute 'count'
으로 실행이 되지 않습니다.
count()를 countDocument()로 바꿔도 보았는데 해결이 안되어 이렇게 문의드립니다.
def lists():
    page = request.args.get("page", default=1, type = int)
    limit = request.args.get("limit",10,type=int)
 
    board = mongo.db.board
    datas = board.find({}).skip((page-1)*limit).limit(limit)
    
    tot_count = board.find({}).count()

답변 1

답변을 작성해보세요.

1

몽고 DB 3.7 인가 부터 collection 의 count() 함수를 더 이상 사용할 수 없습니다. 

tot_count = board.find({}).count()

이 코드는 아래 처럼 변경되어야 할 것 같습니다.

tot_count = board.count_documents({})

직접 테스트 해보진 않았습니다만

 

아래 공식 문서의 링크를 참고해 보면 count_documents 는 컬렉션 객체에서 지원되는 기능입니다. 그래서 board.find() 에 의해 반환되는것은 커서 객체이므로 find({}).count_documents() 처럼은 사용될 수 없고 컬렉션 객체에서 사용되야 합니다.

https://pymongo.readthedocs.io/en/stable/api/pymongo/collection.html#pymongo.collection.Collection.count_documents