mongodb - How to change datatype of netsted field in mongo document? -


my mongo structure 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 in string want changed topprocesses.cpuutilizationpercent data type float. tried below 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 1 how changed string float in nested mongo documents

you doing right way did not include array element match in query portion of .update():

db.collectionname.find({    "topprocesses":{"$exists":true}}).foreach(function(data){     for(var ii=0;ii<data.topprocesses.length;ii++) {       db.collectionname.update(          {               "_id": data._id,               "topprocesses.processid": data.topprocesses[ii].processid // corrected          },          {              "$set": {                "topprocesses.$.cpuutilizationpercent":                    parsefloat(data.topprocesses[ii].cpuutilizationpercent)              }          }       );   } }) 

so need match in array in order positional $ operator have effect.

you have used "index" value in notation, since producing in loop anyway:

db.collectionname.find({    "topprocesses":{"$exists":true}}).foreach(function(data){     for(var ii=0;ii<data.topprocesses.length;ii++) {        var updoc =  {            "$set": {}       };        var mykey = "topprocesses." + ii + ".cpuutilizationpercent";       updoc["$set"][mykey] = parsefloat(data.topprocesses[ii].cpuutilizationpercent);        db.collectionname.update(          {               "_id": data._id          },          updoc       );   } }) 

which uses matching index , handy there no unique identifier of array element.

also note neither "upsert" or "multi" options should apply here due nature of how processes existing documents.


just "postscript" note this, worthwhile consider bulk operations api of mongodb in versions 2.6 , greater. using these api methods can reduce amount of network traffic between client application , database. obvious improvement here in overall speed:

var bulk = db.collectionname.initializeorderedbulkop(); var counter = 0;  db.collectionname.find({    "topprocesses":{"$exists":true}} ).foreach(function(data){     for(var ii=0;ii<data.topprocesses.length;ii++) {        var updoc =  {            "$set": {}       };        var mykey = "topprocesses." + ii + ".cpuutilizationpercent";       updoc["$set"][mykey] = parsefloat(data.topprocesses[ii].cpuutilizationpercent);        // queue update       bulk.find({ "_id": data._id }).update(updoc);       counter++;        // drain , re-initialize every 1000 update statements       if ( counter % 1000 == 0 ) {           bulk.execute();           bulk = db.collectionname.initializeorderedbulkop();       }   } })  // add rest in queue if ( counter % 1000 != 0 )     bulk.execute(); 

this reduces amount of operations statements sent sever sending once every 1000 queued operations. can play number , how things grouped give significant increase in speed in relatively safe way.


Comments

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -