Подтвердить что ты не робот

Mongoid.limit не работает в mongoid 3.1.x

Я пробовал что-то подобное в рельсах с mongoid 3.1.0 и lastest 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

мс)

какая версия mongoid хороша с лимитом()?

4b9b3361

Ответ 1

Команда limit работает нормально, но по какой-то причине count игнорирует предел. Если вы отбросите его в массив, вы увидите, что предел работает.

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

Кроме того, если вы на самом деле перебираете объекты, вы увидите, что предел работает.

Ответ 3

Для 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