使用条件更新mongodb中所有集合的子集

使用条件更新mongodb中所有集合的子集,mongodb,Mongodb,我正在尝试更新mongoDB中的集合。结构如下: obj_100 { id: 123, name: test_1, type : [{ name: test_1, version : 34, } { name: test_65, version : 344, }] } { id: 123, name: production_1, type : [{

我正在尝试更新mongoDB中的集合。结构如下:

obj_100

{
    id: 123,
    name: test_1,
    type : [{
        name: test_1,
        version : 34,
    }
   {
        name: test_65,
        version : 344,
    }]
}
{
    id: 123,
    name: production_1,
    type : [{
        name: production_1,
        version : 34,
    }
    {
        name: production_65,
        version : 344,
    }] 
}
obj_101

{
    id: 124,
    name: test_2,
    type: [{
        name: test_2,
        version: 3,
    }
    {
        name: test_25,
        version: 323,
    }]
}
....
....
{
    id: 124,
    name: production_2,
    type: [{
        name: production_2,
        version: 3,
    }
    {
        name: production_25,
        version: 323,
    }]
}
....
....
现在,我想将
name
中的字符串“test”(无论它在哪里-在本例中,它位于所有集合中的
{obj{name}}
{obj{type{name}}}
下)替换为“production”,这样看起来

obj_100

{
    id: 123,
    name: test_1,
    type : [{
        name: test_1,
        version : 34,
    }
   {
        name: test_65,
        version : 344,
    }]
}
{
    id: 123,
    name: production_1,
    type : [{
        name: production_1,
        version : 34,
    }
    {
        name: production_65,
        version : 344,
    }] 
}
obj_101

{
    id: 124,
    name: test_2,
    type: [{
        name: test_2,
        version: 3,
    }
    {
        name: test_25,
        version: 323,
    }]
}
....
....
{
    id: 124,
    name: production_2,
    type: [{
        name: production_2,
        version: 3,
    }
    {
        name: production_25,
        version: 323,
    }]
}
....
....
我试过这个


$MONGO_ROOT/bin/MONGO 127.0.0.1:27017/cyclops--eval'db.stages.find().forEach(函数(doc){doc.name=doc.name.replace(/msmaster2int/,“test”);db.stages.save(doc);'

上面只是更改了“名称”。同样,我也想更新数组中的数据


有人能帮我一下吗

你应该在更新中使用
multi:true
,如下所示:

db.stages.update({"name":/test/,"type.name":/test/},{"$set":{"name":"production","type.name":"production"}},false,true )
在这里,我将
name:test
更新为
name:production
,首先使用正则表达式搜索
name:test
,并使用新字符串替换它。但在查看编辑的问题和预期输出后,我认为您应该使用以下脚本使用操作

var bulk = db.collectionName.initializeOrderedBulkOp();
var counter = 0;

db.collectionName.find().forEach(function(data) {
    var objectName = data.name; // first get name from outside object
    var regex = /(test_)(\d)/;
    objectName = objectName.replace(regex, "production_$2"); //replace name to production


    for(var ii = 0; ii < data.type.length; ii++) { //iterate over type arry
      var typeName = data.type[ii].name; // find type array name 
      var regex = /(test_)(\d)/;
      typeName = typeName.replace(regex, "production_$2"); //replace same here 

      var typeKey = "type." + ii + ".name";
      var updoc = {
        "$set": {}
      };

      updoc["$set"]["name"] = objectName; // set both name and type.name 
      updoc["$set"][typeKey] = typeName;

      bulk.find({
        "_id": data._id
      }).update(updoc);

      counter++;

      if(counter % 1000 == 0) {
        bulk.execute(); //execute bulk update here
        bulk = db.collectionName.initializeOrderedBulkOp(); 
      }

    }

  })

if(counter % 1000 != 0) bulk.execute();
var bulk=db.collectionName.initializeOrderedBulkOp();
var计数器=0;
db.collectionName.find().forEach(函数(数据){
var objectName=data.name;//首先从外部对象获取名称
var regex=/(test_u2;)(\d)/;
objectName=objectName.replace(regex,“production_$2”);//将名称替换为production
对于(var ii=0;ii
hi Yogesh,上面又写着“0行更新”…你能解释一下你在做什么吗?这是什么脚本?shell脚本?@LogeshwariRam不是shell脚本它是Java脚本,只需在mongo shell中复制上面的代码并检查文档是否更新。我想要一个python脚本。脚本中有很多关于这个mongdb的代码。hve可以将这些代码附加到同一脚本中。你能吗如果可能,请根据python进行更改?$MONGO_ROOT/bin/MONGO 127.0.0.1:27017/cyclops--eval'db.stages.find().forEach({name:/msmaster2int/}).forEach(函数(doc){doc.consumers.forEach(函数(事件){event.name=event.name.name.replace(/msmaster2int/,“test”);});db.collection.save(doc);'