How to change the type of a field?

后端 未结 13 2232
北荒
北荒 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:32

    I need to change datatype of multiple fields in the collection, so I used the following to make multiple data type changes in the collection of documents. Answer to an old question but may be helpful for others.

    db.mycoll.find().forEach(function(obj) { 
    
        if (obj.hasOwnProperty('phone')) {
            obj.phone = "" + obj.phone;  // int or longint to string
        }
    
        if (obj.hasOwnProperty('field-name')) {
         obj.field-name = new NumberInt(obj.field-name); //string to integer
        }
    
        if (obj.hasOwnProperty('cdate')) {
            obj.cdate = new ISODate(obj.cdate); //string to Date
        }
    
        db.mycoll.save(obj); 
    });
    

提交回复
热议问题