How to change the type of a field?

后端 未结 13 2046
北荒
北荒 2020-11-22 15:10

I am trying to change the type of a field from within the mongo shell.

I am doing this...

db.meta.update(
  {\'fields.properties.default\': { $type :         


        
相关标签:
13条回答
  • 2020-11-22 15:36

    Convert String field to Integer:

    db.db-name.find({field-name: {$exists: true}}).forEach(function(obj) { 
        obj.field-name = new NumberInt(obj.field-name);
        db.db-name.save(obj);
    });
    

    Convert Integer field to String:

    db.db-name.find({field-name: {$exists: true}}).forEach(function(obj) {
        obj.field-name = "" + obj.field-name;
        db.db-name.save(obj);
    });
    
    0 讨论(0)
提交回复
热议问题