Tuesday, April 3, 2012

C# and MongoDb tips – Part 3


Changing values in a collection
You can update/ save values in a collection using the Save, Update or FindAndModify methods. The Save method is a combination of Insert and Update. If the Id member of the document has a value, then it is assumed to be an existing document and Save calls Update on the document. Otherwise it is assumed to be a new document and Save calls Insert after first assigning a newly generated unique value to the Id member.
[TestMethod]
public void SaveShouldSaveTheChangedEntityToCollection()
{
    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);
    employee.FirstName = "Rachel";
    employees.Save(employee);
    query = Query.And(Query.EQ("FirstName", "Rachel"), Query.EQ("LastName", "Green"));
    employee = employees.FindOne(query);
    Assert.IsTrue(employee != null && employee.FirstName == "Rachel");
}
The Update method is used to update existing documents.
 [TestMethod]
public void UpdateShouldUpdateTheChangedEntitiesMacthingTheQueryBuilderConditionToCollection()
{
    var database = GetDatabaseInstance();
    var collectionSettings = database.CreateCollectionSettings<Employee>("Employees");
    collectionSettings.SlaveOk = true;
    var employees = database.GetCollection(collectionSettings);
    var query = Query.And(Query.EQ("FirstName", "Rachel"), Query.EQ("LastName", "Green"));
    employees.Update(query, Update.Set("FirstName", "Betty"));
    query = Query.And(Query.EQ("FirstName", "Betty"), Query.EQ("LastName", "Green"));
    var employee = employees.FindOne(query);
    Assert.IsTrue(employee != null && employee.FirstName == "Betty");
}
FindAndModify always updates a single document, and you can combine a query that matches multiple documents with sort criteria that will determine exactly which matching document is updated. In addition, FindAndModify will return the matching documentsand if you wish you can specify which fields of the matching document to return.
[TestMethod]
public void FindAndModifyShouldFindAndModifyTheCollectionInAtomicOperation()
{
    var database = GetDatabaseInstance();
    var collectionSettings = database.CreateCollectionSettings<Employee>("Employees");
    collectionSettings.SlaveOk = true;
    var employees = database.GetCollection(collectionSettings);

    var query = Query.Or(Query.EQ("FirstName", "Mike"), Query.EQ("FirtName", "Steve"));
    var sortOrder = SortBy.Ascending("FirstName", "LastName");
    var update = Update.Set("DoJ", new DateTime(1995, 8, 1));
    var updatedEmployees = employees.FindAndModify(query, sortOrder, update, true);
    var employeesModified = updatedEmployees.GetModifiedDocumentAs<Employee>();

    Assert.IsTrue(employeesModified.DoJ.Date.Year == 1995);
}

No comments: