Mongodb指定字段批量修改

1.第一种

db.attendance.find({'tenantId':10125}).forEach(function(dev) {
        if(dev.idPortrait != null) {
                dev.idPortrait = dev.idPortrait.replace('192.168.1.39:9090','123456789');
                        db.attendance.update({"_id":dev._id},{"$set":{"idPortrait":dev.idPortrait}});
        } 
})

2. 第二种

db.attendance.find({'tenantId':10031}).forEach( 
  function(item) {
      var tmp = String(item.idPortrait);
      tmp = tmp.replace('123456789','192.168.1.39:9090');
      if (tmp == 'undefined'){
          item.idPortrait = '123';
      } else {
          item.idPortrait = tmp;
      }
      db.attendance.save(item);
  }
);

 

你可能感兴趣的:(MongoDB)