Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top
public async void Execute()
{
    var mongoClient = new MongoClient(connectionStringBuilder.ToMongoUrl());
    var db = mongoClient.GetDatabase(connectionStringBuilder.DatabaseName);
    var jobInfoDocuments = db.GetCollection<JobInfoRecord>("JobInfoRecords");

    var encryptedKeys = new[] { "AccountKey", "PrivateKey", "APIKey", "DefectiveKeyGracefulExpiration" };
    var keyPatternMatching = string.Format("({0})", string.Join("|", encryptedKeys));

    // This string is used to scan tags (in the xml string) that contain "password" in their names (case-insensitive match mode),
    // and the tags which are called exactly the same as in the encryptedKeys
    var regex = string.Format(@"(?si)<([^\s<]*password[^\s<]*|{0})>.*?</\1>", keyPatternMatching);

    var filter = Builders<JobInfoRecord>.Filter.Regex(x => x.SerializedBackgroundJobInfo, new BsonRegularExpression(regex));
    var requiredDocuments = await jobInfoDocuments.Find(filter).ToListAsync();
    foreach (var document in requiredDocuments)
    {
        const string EmptyTag = "<$1></$1>";

        var jobIdFilter = Builders<JobInfoRecord>.Filter.Eq("_id", document.JobId);
        var newInfo = Regex.Replace(document.SerializedBackgroundJobInfo, regex, EmptyTag);
        var update = Builders<JobInfoRecord>.Update.Set("SerializedBackgroundJobInfo", newInfo);

        jobInfoDocuments.UpdateOneAsync(jobIdFilter, update).Wait();
     }
}

I'm changing the values of tags in xml string that contains "password" in their names (or called exactly the same as in the encryptedKeys) to empty tag.

I have a collection with 500 000 documents, of which I am performing updates each minute (hopefully) of approx. 3000 documents.

Do I have a bugs? Maybe I can increase performance of the update operation?

I'm using Mongo driver 2.0.2

share|improve this question

One thing that I would probably do differently is move the const string EmptyTag outside of the foreach loop, I don't think that you need to recreate this string every time you loop, move it to the beginning of the method.

Another thing that I would do, make sure that my connections are properly disposed of in all cases by using a using block where ever I could.

Mongo doesn't make use of the IDisposable interface, so you can't use the using syntax to dispose of the connection

I also removed the comment block because I could see what was being done rather easily.

Here is what I came up with,

public async void Execute()
{
    const string EmptyTag = "<$1></$1>";

    var mongoClient = new MongoClient(connectionStringBuilder.ToMongoUrl());
    var db = mongoClient.GetDatabase(connectionStringBuilder.DatabaseName);
    var jobInfoDocuments = db.GetCollection<JobInfoRecord>("JobInfoRecords");
    var encryptedKeys = new[] { "AccountKey", "PrivateKey", "APIKey", "DefectiveKeyGracefulExpiration" };
    var keyPatternMatching = string.Format("({0})", string.Join("|", encryptedKeys));
    var regex = string.Format(@"(?si)<([^\s<]*password[^\s<]*|{0})>.*?</\1>", keyPatternMatching);

    var filter = Builders<JobInfoRecord>.Filter.Regex(x => x.SerializedBackgroundJobInfo, new BsonRegularExpression(regex));
    var requiredDocuments = await jobInfoDocuments.Find(filter).ToListAsync();
    foreach (var document in requiredDocuments)
    {
        var jobIdFilter = Builders<JobInfoRecord>.Filter.Eq("_id", document.JobId);
        var newInfo = Regex.Replace(document.SerializedBackgroundJobInfo, regex, EmptyTag);
        var update = Builders<JobInfoRecord>.Update.Set("SerializedBackgroundJobInfo", newInfo);

        jobInfoDocuments.UpdateOneAsync(jobIdFilter, update).Wait();
    }
}
share|improve this answer
1  
Cannot implicitly convert MongoClient to IDisposable – Anatoly Mar 22 at 13:25
    
what about the db? – Malachi Mar 22 at 13:26
    
The same situation – Anatoly Mar 22 at 13:26
    
must not be the same kind of database that I am used to working with so the connections aren't the same. – Malachi Mar 22 at 13:27

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.