Permalink
Browse files

fix cursor count for find by id + limit

  • Loading branch information...
1 parent 7e154db commit d741920d6adf03b7d1266d990ac8c3f5573f95ef @mislav committed May 7, 2011
Showing with 19 additions and 4 deletions.
  1. +6 −1 lib/mingo.rb
  2. +13 −3 lib/mingo/cursor.rb
View
7 lib/mingo.rb
@@ -254,7 +254,12 @@ class User < Mingo
it "handles limit + skip" do
cursor = described_class.find([@doc3.id, @doc1.id, @doc2.id]).limit(1).skip(2)
cursor.to_a.should == [@doc2]
- cursor.selector[:_id]["$in"].should == [@doc2.id]
+ end
+
+ it "returns correct count" do
+ cursor = described_class.find([@doc3.id, @doc1.id, @doc2.id]).limit(1).skip(2)
+ cursor.next_document
+ cursor.count.should == 3
end
end
View
16 lib/mingo/cursor.rb
@@ -34,9 +34,10 @@ def by_ids?
def next_document
if !@query_run && by_ids? && !order
- limit_ids
- preload_cache
- sort_cache_by_ids
+ limit_ids do
+ preload_cache
+ sort_cache_by_ids
+ end
end
super
end
@@ -65,8 +66,17 @@ def reverse
def limit_ids
if @limit > 0 || @skip > 0
ids = selector[:_id]["$in"]
+ old_skip = @skip
selector[:_id]["$in"] = ids[@skip, @limit > 0 ? @limit : ids.size]
@skip = 0
+ begin
+ yield
+ ensure
+ @skip = old_skip
+ selector[:_id]["$in"] = ids
+ end
+ else
+ yield
end
end

0 comments on commit d741920

Please sign in to comment.