IT博客汇
  • 首页
  • 精华
  • 技术
  • 设计
  • 资讯
  • 扯淡
  • 权利声明
  • 登录 注册

    mongo修改字段类型(string to int)

    mckee发表于 2015-11-25 20:56:09
    love 0

    mongo可以通过find(...).forEach(function(x) {})语法来修改collection的field类型。
    假设collection为foo,field为bad:
    转换为int类型:

    db.foo.find({bad: {$exists: true}}).forEach(function(obj) { 
        obj.user_id = new NumberInt(obj.user_id);
        db.foo.save(obj);
    });

    同理转换为string类型:
    db.foo.find( { bad : { $exist : true } } ).forEach( function (x) { 
      x.bad = new String(x.bad); // convert field to string 
      db.foo.save(x); 
    });



沪ICP备19023445号-2号
友情链接