Efficiently update string (in XML format) in MongoDb public async void Execute()
{
    var mongoClient = new MongoClient(connectionStringBuilder.ToMongoUrl());
    var db = mongoClient.GetDatabase(connectionStringBuilder.DatabaseName);
    var jobInfoDocuments = db.GetCollection&lt;JobInfoRecord&gt;("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)&lt;([^\s&lt;]*password[^\s&lt;]*|{0})&gt;.*?&lt;/\1&gt;", keyPatternMatching);

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

        var jobIdFilter = Builders&lt;JobInfoRecord&gt;.Filter.Eq("_id", document.JobId);
        var newInfo = Regex.Replace(document.SerializedBackgroundJobInfo, regex, EmptyTag);
        var update = Builders&lt;JobInfoRecord&gt;.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
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 = "&lt;$1&gt;&lt;/$1&gt;";

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

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

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