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