简体   繁体   English

在MongoDB集合中搜索和替换

[英]Search and replace in MongoDB collection

I have a mongo collection that contains a field called "url", storing an url. 我有一个mongo集合,其中包含一个名为“ url”的字段,用于存储URL。 Some of those urls have an extra parameter that bothers me, "id=<>" where <> can have any size Can i do this in a update operation or do i need to write a script that will iterate and replace? 其中一些url有一个令我烦恼的额外参数,“ id = <>”,其中<>可以具有任意大小。我可以在更新操作中执行此操作吗?还是需要编写将迭代并替换的脚本?

db.find({{"url": /.&id=.*/ }}).update(???)

Your best bet is going to be to explore use of the "multi" parameter in conjunction with field update parameters, as described at these locations on the MongDB website. 最好的选择是像MongDB网站上的这些位置所述,探索结合使用“ multi”参数和字段更新参数。

http://docs.mongodb.org/manual/reference/operator/update-field/ http://docs.mongodb.org/manual/reference/method/db.collection.update/#multi-parameter http://docs.mongodb.org/manual/reference/operator/update-field/ http://docs.mongodb.org/manual/reference/method/db.collection.update/#multi-parameter

An example is provided here: 这里提供一个示例:

http://docs.mongodb.org/manual/reference/method/db.collection.update/#example-update-multi http://docs.mongodb.org/manual/reference/method/db.collection.update/#example-update-multi

The issue is that you won't be able to implement any sort of "capturing group" functionality that just gets rid of the "id" parameter for each document though. 问题是,您将无法实现仅摆脱每个文档的“ id”参数的任何一种“捕获组”功能。 There is a ticket open on MongoDB's jira account though detailing this behavior (currently in unresolved state): 虽然详细说明了此行为(目前处于未解决状态),但MongoDB的jira帐户上有一张待售票:

https://jira.mongodb.org/browse/SERVER-9159 https://jira.mongodb.org/browse/SERVER-9159

Try this to find documents using this regEx and then do update accordingly. 尝试使用此regEx查找文档,然后进行相应更新。

db.find({
    "url": /id=\<.+\>/
}, function(err, result) {
    //your code
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM