我有一个Mongodb集合parking_info,这是集合中的一个文档:
{
"_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)"
}
我需要转换最后一个字段"LOCATION" : "(37.8007983983, -122.4368696024)"
这里的顺序是纬度,然后是经度。
到
"LOCATION" : { type: "Point", coordinates: [ -122.4368696024, 37.8007983983]},
坐标顺序是经度,然后是纬度。
如何将其应用于集合中的所有文档?
答案 0 :(得分:1)
您可以使用 forEach()
方法返回的光标方法 find()
来迭代光标,访问文档以及每个文档使用本机JavaScript方法修改LOCATION字段,如以下示例所示:
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);
});