在Mongodb中搜索和替换? [英] Search and replace in Mongodb?
问题描述
给定一组100个帖子,每个帖子都有一个带有帖子内容的body属性,并且在该内容中有图像网址,如 http://example.com/wp-content/uploads/5.jpg
Given a set of 100 posts, and each post having a a body attribute with post content and inside that content there are image url's like "http://example.com/wp-content/uploads/5.jpg"
有没有办法浏览每个帖子的正文内容,然后查找符合 http://示例的内容.com / wp-content / uploads / 5.jpg 并将其替换为 http://amazon-bucket.aws.com/wp-content/uploads/5.jpg
Is there a way to go through each post's body content and then look for anything that matches the "http://example.com/wp-content/uploads/5.jpg" and replace it with something like "http://amazon-bucket.aws.com/wp-content/uploads/5.jpg"
谢谢!
推荐答案
不完全如此,我的意思是,如果你不是在寻找确切的字符串并且想要总是替换为相同的不同字符串。
Not exactly, and by that I mean if you were not looking for the "exact string" and wanting to always replace with the "same" different string.
基本上看起来你正在为可能的文件寻找正则表达式替换通过 .update()
执行。虽然可以 $ regex
搜索,没有捕获或选项将捕获的部分提供给语句的更新部分,例如 $ set
。
Essentially it looks like you are looking for a "regex replace" for documents that can be performed via .update()
. While it is possible to $regex
search, there is no "capture" or option to feed captured portions to the "update" part of a statement such as $set
.
因此,为了进行此类更新,您需要循环文档并在代码中进行修改。但批量操作API 可以在这方面提供一些帮助:
So in order to do this sort of update, you need to loop your documents and modify in code. But the Bulk Operations API can be of some assistance here:
var bulk = db.collection.initializeOrderedBulkOp();
var counter = 0;
var query = { "url": { "$regex": "^http://example\.com" }};
db.collection.find(query).forEach(function(doc) {
// Inspect and replace the part of the string
bulk.find({ "_id": doc._id }).updateOne(
{ "$set": { "url": doc.url.replace("example.com","bucket.aws.com") } }
);
counter++;
// Update once every 1000 documents
if ( counter % 1000 == 0 ) {
bulk.execute();
bulk = db.collection.initializeOrderedBulkOp();
}
})
// Process any remaining
if ( counter % 1000 != 0 )
bulk.execute();
因此仍需要循环,但至少每1000个文档处理一次,更新才会发送到服务器。
So that still requires looping but at least the updates are only sent to the server once every 1000 documents processed.
这篇关于在Mongodb中搜索和替换?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!