Skip to content

Commit

Permalink
Query hints are no longer erased on explain. [ fix mongoid#84 ]
Browse files Browse the repository at this point in the history
  • Loading branch information
durran committed Oct 6, 2012
1 parent 7de7c50 commit 71c21c5
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

* \#87 `Moped::BSON::ObjectId.legal?` now returns true for object ids.

* \#84 Query hints are no longer wiped on explain.

* \#60/\#80 Moped now gracefully handles replica set reconfig and crashes of the
primary and secondary. By default, the node list will be refreshed every
second and the operation will be retried up to 30 times. This is configurable
Expand Down
7 changes: 5 additions & 2 deletions lib/moped/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,15 @@ def each
#
# @since 1.0.0
def explain
hint, sort = operation.selector["$hint"], operation.selector["$orderby"]
operation.selector = {
"$query" => selector,
"$orderby" => operation.selector.fetch("$orderby", {}),
"$explain" => true,
"$limit" => operation.selector.fetch("$limit", 1).abs * -1
} and each { |doc| return doc }
}
operation.selector["$orderby"] = sort if sort
operation.selector["$hint"] = hint if hint
each { |doc| return doc }
end

# Get the first matching document.
Expand Down
39 changes: 38 additions & 1 deletion spec/moped/query_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,6 @@
it "updates to a mongo advanced selector" do
operation.selector.should eq(
"$query" => { created_at: { "$exists" => false }},
"$orderby" => {},
"$explain" => true,
"$limit" => -1
)
Expand All @@ -426,6 +425,44 @@
explain["nscannedObjects"].should eq(2)
end
end

context "when a hint exists" do

before do
2.times do |n|
users.insert({ likes: n })
end
end

let(:explain) do
users.find(likes: { "$exists" => false }).hint(_id: 1).explain
end

let(:stats) do
Support::Stats.collect { explain }
end

let(:operation) do
stats[node_for_reads].grep(Moped::Protocol::Query).last
end

it "updates to a mongo advanced selector" do
operation.selector.should eq(
"$query" => { likes: { "$exists" => false }},
"$explain" => true,
"$hint" => { _id: 1 },
"$limit" => -1
)
end

it "scans more than one document" do
explain["nscanned"].should eq(2)
end

it "scans more than one object" do
explain["nscannedObjects"].should eq(2)
end
end
end

describe "#each" do
Expand Down

0 comments on commit 71c21c5

Please sign in to comment.