Skip to content
This repository has been archived by the owner on Jan 2, 2024. It is now read-only.

Commit

Permalink
Fix explain
Browse files Browse the repository at this point in the history
  • Loading branch information
lanwin committed Dec 14, 2010
1 parent dcb7912 commit 2ffb790
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 4 deletions.
15 changes: 12 additions & 3 deletions source/MongoDB.Tests/IntegrationTests/TestCursor.cs
Expand Up @@ -114,12 +114,21 @@ public void TestCanReadSmall()
}

[Test]
public void TestExplain()
public void TestExplainWithSort()
{
var exp = DB["reads"].FindAll().Limit(5).Skip(5).Sort("x").Explain();
Assert.IsTrue(exp.ContainsKey("cursor"));
Assert.IsTrue(exp.ContainsKey("n"));
Assert.IsTrue(exp.ContainsKey("nscanned"));
Assert.AreEqual(9999, exp.Get("nscanned"));
Assert.AreEqual(10,exp.Get("n"));
}

[Test]
public void TestExplain()
{
var exp = DB["reads"].FindAll().Limit(5).Skip(5).Explain();
Assert.IsTrue(exp.ContainsKey("cursor"));
Assert.AreEqual(10, exp.Get("nscanned"));
Assert.AreEqual(5, exp.Get("n"));
}

[Test]
Expand Down
7 changes: 6 additions & 1 deletion source/MongoDB/Cursor_1.cs
Expand Up @@ -207,8 +207,11 @@ public ICursor<T> KeepCursor(bool value)
/// <returns></returns>
public Document Explain(){
TryModify();
_specOpts["$explain"] = true;

var savedLimit = _limit;

_specOpts["$explain"] = true;
_limit = _limit * -1;
var explainResult = RetrieveData<Document>();
try
{
Expand All @@ -223,6 +226,8 @@ public ICursor<T> KeepCursor(bool value)
{
if(explainResult.CursorId > 0)
KillCursor(explainResult.CursorId);

_limit = savedLimit;
}
}

Expand Down

0 comments on commit 2ffb790

Please sign in to comment.