mongo 可以通过ind(…).forEach(function(x) {})语法来修改collection的field的类型。

假设collection为foo,field为bad:

string 转 int

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

int 转 string

db.foo.find(bad: {$exists: true}).forEach(function(obj) {
    obj.bad = obj.bad + "";
    db.foo.save(obj);
});
Scroll to Top