Modifying a cursor
The Find method doesn’t immediately return the actual
results of a query. Instead they return a cursor (MongoCursor ) that can be enumerated to retrieve the results
of the query. The query is sent to the server when we first try to retrieve the
result. This feature allows the user to control the results of the query before
fetching the results by modifying the cursor.
For e.g. you can use the skip, limit, sort properties to
modify the cursor. You can also use the fluent interface methods for the
properties to modify the cursor. After setting these properties, you can
enumerate the results to get the actual output.
[TestMethod]
public void
SkipAndLimitShouldReturnPagedDataFromACursor()
{
var database = GetDatabaseInstance();
var collectionSettings =
database.CreateCollectionSettings<Employee>("Employees");
collectionSettings.SlaveOk = true;
var employees = database.GetCollection(collectionSettings);
var employeeCursor = employees.FindAll();
employeeCursor.Skip = 5;
employeeCursor.Limit = 2;
Assert.IsTrue(employeeCursor.ToList().Count == 2);
}
[TestMethod]
public void
SkipAndLimitUsingFluentInterfacesShouldReturnPagedDataFromACursor()
{
var database = GetDatabaseInstance();
var collectionSettings =
database.CreateCollectionSettings<Employee>("Employees");
collectionSettings.SlaveOk = true;
var employees =
database.GetCollection(collectionSettings);
var employeeCursor = employees.FindAll().SetSkip(5).SetLimit(2).SetSortOrder("FirstName", "LastName");
Assert.IsTrue(employeeCursor.ToList().Count == 2);
}
No comments:
Post a Comment