2013-04-18 11 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のパラメータについてGallery.limit(1).count(true)

+2

@Deanあなたのリンクは死んでいます – Geoffroy

+0

@Geoffroyリンクを修正しました! –

0

を使用する必要があります。

# 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