const string Pattern = @"(?si)<([^\s<]*totalWork[^\s<]*)>.*?</\1>";
var filter = Builders<JobInfoRecord>.Filter.Regex(x => x.SerializedBackgroundJobInfo,
new BsonRegularExpression(Pattern, "i"));
var documents = await records.Find(filter).ToListAsync();
====
在我得到documents
后,我处理了我身边的每个文件。
const string EmptyTag = "<$1></$1>";
var updatedJobInfo = Regex.Replace(document.SerializedBackgroundJobInfo, Pattern, EmptyTag);
我怎样才能在mongo方面做Regex.Replace
?或者那只能发生在客户端?
以下Replace
是否适用于Mongo方面?
using (var cursor = await jobInfoDocuments.FindAsync<JobInfoRecord>(filter))
{
while (await cursor.MoveNextAsync())
{
var batch = cursor.Current;
foreach (var document in batch)
{
var newInfo = Regex.Replace(document.SerializedBackgroundJobInfo, regex, EmptyTag);
// Applying several operations within the one request.
operationList.Add(new UpdateOneModel<JobInfoRecord>(Builders<JobInfoRecord>.Filter.Eq("_id", document.JobId),
Builders<JobInfoRecord>.Update.Set("SerializedBackgroundJobInfo", newInfo)));
}
答案 0 :(得分:1)
您可以使用javascript
执行此操作,但请确保修复filter
以使用mongo shell
db.records.find(filter).forEach(function (doc) {
var pattern = /<([^\s<]*totalWork[^\s<]*)>[\s\S]*?</\1>/i;
var EmptyTag = "<$1></$1>";
doc.SerializedBackgroundJobInfo = doc.SerializedBackgroundJobInfo.replace(pattern, EmptyTag);
db.records.save(doc);
})