Tuesday, April 3, 2012

C# and MongoDb tips – Part 2


Filtering a collection
To retrieve documents from a collection use one of the various Find methods. FindOne returns the first document it finds (when there are many documents in a collection you can't be sure which one it will be).
[TestMethod]
public void FindOneShouldReturnTheFirstEntryInTheCollection()
{
    var database = GetDatabaseInstance();
    var collectionSettings = database.CreateCollectionSettings<Employee>("Employees");
    collectionSettings.SlaveOk = true;
    var employees = database.GetCollection(collectionSettings);

    var firstEmployee = employees.FindOne();
    Assert.IsTrue(firstEmployee.Id != default(int));
}

If you want to read a document that is not of the type use the FindOneAs method, which allows you to override the type of the returned document.
[TestMethod]
public void FindOneAsShouldReturnTheFirstItemInTheCollectionOverridingTheTypeOfReturnedDocument()
{
    var database = GetDatabaseInstance();
    var employees = database.GetCollection("Employees");
    var employee = employees.FindOneAs(typeof (Employee));
    Assert.IsTrue(employee != null && employee is Employee);
}

The Find and FindAs methods take a query that tells the server which documents to return. The query parameter is of type IMongoQuery. IMongoQuery is a marker interface that identifies classes that can be used as queries. The most common ways to construct a query are to either use the Query builder class.
[TestMethod]
public void FindOneShouldReturnTheFirstItemInTheCollectionBasedOnQuery()
{
    var database = GetDatabaseInstance();
    var collectionSettings = database.CreateCollectionSettings<Employee>("Employees");
    collectionSettings.SlaveOk = true;
    var employees = database.GetCollection(collectionSettings);
    var queryDocument = new QueryDocument("FirstName", "Betty");
    var employee = employees.FindOne(queryDocument);
    Assert.IsTrue(employee != null && employee.FirstName == "Betty");
}

[TestMethod]
public void QueryBuilderCanBeUsedToFilterEntriesInCollection()
{
    var database = GetDatabaseInstance();
    var collectionSettings = database.CreateCollectionSettings<Employee>("Employees");
    collectionSettings.SlaveOk = true;
    var employees = database.GetCollection(collectionSettings);
    var query = Query.And(Query.EQ("FirstName", "Betty"), Query.EQ("LastName", "Green"));
    var employee = employees.FindOne(query);
    Assert.IsTrue(employee != null && employee.FirstName == "Betty");
}

[TestMethod]
public void FindReturnsAllEntriesMatchingTheQueryInTheCollection()
{
    var database = GetDatabaseInstance();
    var collectionSettings = database.CreateCollectionSettings<Employee>("Employees");
    collectionSettings.SlaveOk = true;
    var employees = database.GetCollection(collectionSettings);

    var query = Query.Or(Query.EQ("FirstName", "Betty"), Query.EQ("FirstName", "Prajeesh"));

    var employeeResult = employees.Find(query);
    Assert.IsTrue(employeeResult.All(e => e.FirstName == "Betty" || e.FirstName == "Prajeesh"));
}

No comments: