13

私はmongoid 3.1.0と最新の3.1.3を使ってレールでこのようなことを試しました。.limit は機能しません。その下では1行を返す必要がありますが、すべてを返します(4)

コード:

@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)

limit() に適した mongoid のバージョンはどれですか?

4

3 に答える 3

4

Mongoid 5 の場合、パラメータがCollectionView#count変更されました:

    # 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
于 2016-05-19T20:20:50.453 に答える