假设像这样的mongodb集合模式:
{
"_id" : ObjectId("5a5b2657a19692e18a3792ad"),
"Toponym" : "Climate station Stavenhagen",
"Lat" : 53.33333,
"Lon" : "13.99999",
"SensorMaker" : "Hitachi",
"SensorClass" : "Thermometer",
"Dimension" : "SoilTemperature_0.05mSensor1",
"Gauge" : "degC"
}
我想将完整的集合(~90k项目)更改为此,以符合最小的GeoJson:
{
"_id" : ObjectId("5a5b2657a19692e18a3792ad"),
"Toponym" : "Climate station Stavenhagen",
"geometry" : {
"type" : "Point",
"coordinates" : [53.33333, 13.99999]
},
"SensorMaker": "Hitachi",
"SensorClass": "Thermometer",
"Dimension" : "SoilTemperature_0.05mSensor1",
"Gauge" : "degC"
}
我尝试使用此查询转换它,但无论我做什么,我都会收到类似的错误"第5行:意外的字符串":
db.sensor_geo.aggregate([
{ '$group' : {
'_id' : '$_id',
'Toponym' : '$Toponym'
'geometry': { 'type': 'Point', { $set : {"coordinates.$[]": [ {'$Lat', '$Lon'} ] }}},
'SensorMaker' : '$SensorMaker',
'SensorClass' : '$SensorClass',
'Dimension' : '$Dimension',
'Gauge' : '$Gauge'
}
}
]);
我应该使用$push
代替$set
,即使这也无处可去吗?我是否还必须为嵌套的Object创建一个ObjectID,这可能导致了这个问题?
答案 0 :(得分:1)
您可以使用批量写入尝试以下聚合管道。
下面的聚合将Lat和Lon字段更改为具有批量更新的几何图形,以写入新的几何字段并删除Lat和Lon字段。
var bulk = db.getCollection("sensor_geo").initializeUnorderedBulkOp();
var count = 0;
var batch = 1;
db.getCollection("sensor_geo").aggregate([
{"$project":{
"geometry":{
"type":"Point", "coordinates":["$Lat", "$Lon"]
}
}}]).forEach(function(doc){
var _id = doc._id;
var geometry = doc.geometry;
bulk.find({ "_id" : _id }).updateOne(
{
$set: {"geometry":geometry},
$unset: {"Lat":"", "Lon":""}
}
);
count++;
if (count == batch) {
bulk.execute();
bulk = db.getCollection("sensor_geo").initializeUnorderedBulkOp();
count = 0;
}
});
if (count > 0) {
bulk.execute();
}