2013-04-18 2 views
11

mongoid 3.1.0 및 lastest 3.1.3이있는 레일에서 이와 같은 것을 시도했습니다. .limit가 작동하지 않습니다. 그것은 1 개 행을 반환해야하지만, 모든 (4)mongoid .limit는 mongoid에서 작동하지 않습니다. 3.1.x

코드를 반환 아래 :

@go = Gallery.limit(1) 
logger.info "count: #{@go.count}" 

출력 :

count: 4 
MOPED: 54.234.11.193:10055 QUERY database=mongohqtestdatabase collection=galleries selector= {"$query"=>{}, "$orderby"=>{:_id=>1}} flags=[:slave_ok] limit=-1 skip=0 batch_size=nil fields=nil (276.2010 

MS) mongoid의 버전 (제한과 좋은

)?

답변

25

limit 명령이 정상적으로 작동하지만 어떤 이유로 인해 count이 제한을 무시합니다. 배열에 캐스트하면 제한이 작동하는 것을 볼 수 있습니다.

Array(Gallery.limit(1)).length # this gives 1 

또한 실제로 개체를 반복하면 한계가 작동하는 것을 볼 수 있습니다. 공식 Mongoid answer에서 제안한 것처럼

+1

덕분에 뭔가를 할 수 있습니다. 여기에 답변도 있습니다 .https : //github.com/mongoid/mongoid/issues/2981 – Axil

+1

당신이 유용하다고 생각되면 대답을 upvote하고 질문에 대답한다면 그것을 받아들이십시오. – Leopd

14

, 우리는 Mongoid 5 변경 CollectionView#count의 매개 변수에 대해

+2

@Dean 귀하의 링크가 죽었습니다 – Geoffroy

+0

@Geoffroy 링크가 수정되었습니다! –

0

Gallery.limit(1).count(true)를 사용한다 :

# Get a count of matching documents in the collection. 
    # 
    # @example Get the number of documents in the collection. 
    # collection_view.count 
    # 
    # @param [ Hash ] options Options for the count command. 
    # 
    # @option options :skip [ Integer ] The number of documents to skip. 
    # @option options :hint [ Hash ] Override default index selection and force 
    # MongoDB to use a specific index for the query. 
    # @option options :limit [ Integer ] Max number of docs to return. 
    # @option options :max_time_ms [ Integer ] The maximum amount of time to allow the 
    # command to run. 
    # 
    # @return [ Integer ] The document count. 
    # 
    # @since 2.0.0 

그래서 당신은

collection.count(limit: 1) # => 1 
관련 문제