-
Type: Task
-
Resolution: Done
-
Affects Version/s: None
-
Component/s: None
-
None
Hi,
There seems to be some inconsistent behaviour, or a bug, in the size or length method.
For example, using mongoid v 3.0.4 I see get the following:
1.9.3p194 :003 > articles= Article.all.limit(10) => #<Mongoid::Criteria selector: {}, options: {:limit=>10}, class: Article, embedded: false> 1.9.3p194 :004 > articles.size => 975 articles.each {|a| puts a.id} F476xzrwK1h4FW F477BqJ2fYJys4 F477RWVZ0Xa2v0 F477fBf5HWq6ww F477vtrbcW4Ayr F478M6w4Cx9WV4 F478qVF6rvdeYw F479FhJZSJk058 F479mDzC6rbygc F47AvNFdcg3fn0 => #<Mongoid::Contextual::Mongo:0x007fabc5d186e8 @cache=nil, @klass=Article, @criteria=#<Mongoid::Criteria selector: {}, options: {:limit=>10}, class: Article, embedded: false> , @collection=#<Moped::Collection:0x007fabc5d18210 @database=#<Moped::Database:0x007fabc5d18350 @session=#<Moped::Session:0x007fabc5d976a0 @cluster=<Moped::Cluster nodes=[<Moped::Node resolved_address="127.0.0.1:27017">]>, @context=#<Moped::Session::Context:0x007fabc5d96e80 @session=#<Moped::Session:0x007fabc5d976a0 ...>>, @options={:consistency=>:eventual, :database=>"dalis_development"}, @current_database=#<Moped::Database:0x007fabc5d18350 ...>>, @name="dalis_development">, @name=:articles>, @query=#<Moped::Query:0x007fabc5d18198 @selector={}, @collection=#<Moped::Collection:0x007fabc5d18210 @database=#<Moped::Database:0x007fabc5d18350 @session=#<Moped::Session:0x007fabc5d976a0 @cluster=<Moped::Cluster nodes=[<Moped::Node resolved_address="127.0.0.1:27017">]>, @context=#<Moped::Session::Context:0x007fabc5d96e80 @session=#<Moped::Session:0x007fabc5d976a0 ...>>, @options={:consistency=>:eventual, :database=>"dalis_development"}, @current_database=#<Moped::Database:0x007fabc5d18350 ...>>, @name="dalis_development">, @name=:articles>, @operation=#<Moped::Protocol::Query @length=0 @request_id=0 @response_to=0 @op_code=2004 @flags=[] @full_collection_name="dalis_development.articles" @skip=0 @limit=10 @selector={} @fields=nil>>, @length=10, @cache_loaded=true> 1.9.3p194 :018 > articles.size => 10
Unless I iterate through the results, criteria.size() and criteria.length() return the same as criteria.count() but once I've iterated the collection it applies the limit & offset options and so the size of the collection changes.
Neither size() or length() honour either limit() or skip() options if the results have not be iterated over, but do once the collection has been iterated over. Personally I think size(), length() and probably count() should all honour the limit() and offset() options regardless of whether the criteria has been iterated over....