2013-04-18 17 views
11

içinde çalışmaz. Mongoid 3.1.0 ve en son 3.1.3 olan raylarda böyle bir şey denedim. .limit çalışmıyor. Bu 1 satır döndürmelidir ama hepsi (4)mongoid .limit mongoid 3.1.x

kodu verir aşağıdaki:

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

çıkışı:

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 sürümü (limitli iyi

)

cevap

25

limit komutu düzgün çalışıyor, ancak bazı nedenlerle count sınırı yok sayar. Bir diziye attıysanız, sınırın çalıştığını görürsünüz. Aslında nesneler arasında dolaşmak eğer

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

Ayrıca, sınır çalıştığını göreceksiniz. Resmi Mongoid answer önerildiği gibi

+1

teşekkür gibi bir şey yapabilirsiniz. Burada da bir cevap var.https: //github.com/mongoid/mongoid/issues/2981 – Axil

+1

Eğer yararlı bulduysanız bir cevabınızı almak için kibar ve soruya cevap verirse kabul edin. – Leopd

14

, biz Mongoid 5 değişti CollectionView#count parametre için

+2

@Dean bağlantınız kesildi – Geoffroy

+0

@Geoffroy Bağlantıyı düzelt! –

0

Gallery.limit(1).count(true) kullanarak edilmelidir:

# 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 

Yani

collection.count(limit: 1) # => 1 
İlgili konular