2013-04-18 6 views
11

में काम नहीं करता है मैंने मैंगॉयड 3.1.0 और सबसे ऊपर 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 

एमएस)

जो mongoid के संस्करण सीमा के साथ अच्छा (है)?

उत्तर

25

limit कमांड ठीक काम करता है, लेकिन किसी कारण से count सीमा को अनदेखा करता है। यदि आप इसे सरणी में डाल देते हैं तो आप देखेंगे कि सीमा काम कर रही है।

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

इसके अलावा, यदि आप वास्तव में वस्तुओं के माध्यम से पुनरावृत्त करते हैं तो आप देखेंगे कि सीमा काम कर रही है।

+1

धन्यवाद कुछ कर सकते हैं। यहां एक उत्तर भी है। http://github.com/mongoid/mongoid/issues/2981 – Axil

+1

अगर आपको यह उपयोगी लगता है तो यह उत्तर देने के लिए विनम्र है, और यदि यह आपके प्रश्न का उत्तर देता है तो इसे स्वीकार करें। – Leopd

14

आधिकारिक Mongoid answer में सुझाव के रूप में, हम Gallery.limit(1).count(true)

+2

@Dan आपका लिंक मृत है – Geoffroy

+0

@Geoffroy लिंक फिक्स्ड! –

0

का उपयोग करना चाहिए 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