mongo修改字段类型string|mongo修改字段类型(string to int)的教程

更新时间:2020-03-24    来源:php与数据库    手机版     字体:

【www.bbyears.com--php与数据库】

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);
});

本文来源:http://www.bbyears.com/jiaocheng/89397.html