Wednesday, February 18, 2015

How to change data type of a field in mongo

Following query helps you convert the data type of a field from

  • string to integer:

db.my_collection.find().forEach( function(obj) {
    obj.field-name= parseInt(obj.field-name);
    db.my_collection.save(obj);

});


  • and from integer to string : 

db.my_collection.find().forEach( function(obj) {
    obj.field-name= ""+obj.field-name;
    db.my_collection.save(obj);

});

No comments:

Post a Comment