I have a collection with approximately 271K documents. After a mongoimport from a TSV file, all columns were strings. So I need to do two things:
- Trim the strings
- Convert some columns from string to numeric
I am using the following function:
db.hd.find().forEach(function(doc) {
if (typeof doc.HD === "string") {
db.hd.update({_id : doc._id}, {$set : {"HD" : parseInt(doc.HD.trim())}});
}
if (typeof doc.Ptm === "string") {
db.hd.update({_id : doc._id}, {$set: { "Ptm" : parseFloat(doc.Ptm.trim())}});
}
if (typeof doc.n_Ptm === "string") {
db.hd.update({_id : doc._id}, {$set: { "n_Ptm" : doc.n_Ptm.trim()}});
}
if (typeof doc.Ptg === "string") {
db.hd.update({_id : doc._id}, { $set : { "Ptg" : parseFloat(doc.Ptg.trim())}});
}
if (typeof doc.n_Ptg === "string") {
db.hd.update({_id : doc._id }, { $set : { "n_Ptg" : doc.n_Ptg.trim()}});
}
if (typeof doc.SpT === "string") {
db.hd.update({_id : doc._id}, { $set : { "SpT" : doc.SpT.trim()}});
}
if (typeof doc.Int === "string") {
db.hd.update({_id : doc._id}, { $set : { "Int" : doc.Int.trim()}});
}
});
This is a very slow function, obviously because of the collection size and amount of work being performed.
Since I'm a relative newbie at Mongo, is there a faster way to accomplish what I'm doing?
Jason