My Mongo structure as below,
"topProcesses" : [
{
"cpuUtilizationPercent" : "0.0",
"processId" : "1",
"memoryUtilizationPercent" : "0.1",
"command" : "init",
"user" : "root"
},
{
"cpuUtilizationPercent" : "0.0",
"processId" : "2",
"memoryUtilizationPercent" : "0.0",
"command" : "kthreadd",
"user" : "root"
},
{
"cpuUtilizationPercent" : "0.0",
"processId" : "3",
"memoryUtilizationPercent" : "0.0",
"command" : "ksoftirqd/0",
"user" : "root"
},
{
"cpuUtilizationPercent" : "0.0",
"processId" : "5",
"memoryUtilizationPercent" : "0.0",
"command" : "kworker/0:+",
"user" : "root"
},
{
"cpuUtilizationPercent" : "0.0",
"processId" : "6",
"memoryUtilizationPercent" : "0.0",
"command" : "kworker/u3+",
"user" : "root"
},
{
"cpuUtilizationPercent" : "0.0",
"processId" : "8",
"memoryUtilizationPercent" : "0.0",
"command" : "rcu_sched",
"user" : "root"
}
]
Now in above documents topProcesses.cpuUtilizationPercent
is in string and I wanted to change topProcesses.cpuUtilizationPercent
data type to Float
. For this I tried below but it did not work
db.collectionName.find({
"topProcesses":{"$exists":true}}).forEach(function(data){
for(var ii=0;ii<data.topProcesses.length;ii++){
db.collectionName.update({_id: data._id},{$set:{"topProcesses.$.cpuUtilizationPercent":parseFloat(data.topProcesses[ii].cpuUtilizationPercent)}},false,true);
}
})
Can any one help how to changed string to float in nested Mongo documents
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…