1
\$\begingroup\$
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

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

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();
    }
}
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Cannot implicitly convert MongoClient to IDisposable \$\endgroup\$
    – Anatoly
    Commented Mar 22, 2016 at 13:25
  • \$\begingroup\$ what about the db? \$\endgroup\$
    – Malachi
    Commented Mar 22, 2016 at 13:26
  • \$\begingroup\$ The same situation \$\endgroup\$
    – Anatoly
    Commented Mar 22, 2016 at 13:26
  • \$\begingroup\$ must not be the same kind of database that I am used to working with so the connections aren't the same. \$\endgroup\$
    – Malachi
    Commented Mar 22, 2016 at 13:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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