how to change mongodb string to geoJSON point throughout the collection -


i have mongodb collection parking_info , 1 document in collection:

{ "_id" : objectid("559c152fa439a961c357f931"), "post_id" : "354-20160", "ms_id" : "-", "ms_spaceid" : 0, "cap_color" : "grey", "meter_type" : "ss", "smart_mete" : "y", "activesens" : "n", "jurisdicti" : "sfmta", "on_off_str" : "on", "osp_id" : 0, "street_num" : 2016, "streetname" : "chestnut st", "street_seg" : 3.977e+006, "ratearea" : "area 5", "sfparkarea" : "marina", "location" : "(37.8007983983, -122.4368696024)" 

}

i need convert last field "location" : "(37.8007983983, -122.4368696024)" here order latitude , longitude. to

"location" : { type: "point", coordinates: [ -122.4368696024, 37.8007983983]}, coordinate order longitude, latitude.

how can apply documents in collection?

you can use cursor method foreach() returned find() method iterate cursor, access documents, , each document modify location field using native javascript methods in following example:

db.parking_info.find({"location": {"$type": 2}}).foreach(function(doc){     var obj = {},         loc = [],         temp = doc.location.replace("(", "").replace(")", "");     loc = temp.split(",").map(function(point){         return parsefloat(point);     });      obj["type"] = "point";     obj["coordinates"] = loc;     doc.location = obj;     db.parking_info.save(doc);     }); 

Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -