How to convert _id field of all existing documents from type String to Number
db.getCollection('test').find().forEach(function (doc) {
    db.getCollection('test').remove({ _id : doc._id});
    tempId = new NumberLong(doc._id);
    doc._id = tempId;
    db.getCollection('test').save(doc);
    } 
);


This will do. You may want to add error handling as per your use case. Also make sure you have backup before migrating.