【发布时间】:2016-07-08 01:36:04
【问题描述】:
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)));
}
【问题讨论】:
-
哎哟!在发布赏金之前,显然应该将这个问题作为重复问题关闭。请参阅MongoDB: Updating documents using data from the same document,这意味着“循环”,而不是接受答案中字段重命名的轻微重组。这里唯一实际要做的是你已经得到的答案How to increase performance of the update operation in Mongo?
-
Regex.replace 在 MongoDB 服务器端不支持,所以如果您有特殊需求,必须在客户端完成。