问题
Suppose a mongodb collection schema like this:
{
"_id" : ObjectId("5a5b2657a19692e18a3792ad"),
"Toponym" : "Climate station Stavenhagen",
"Lat" : 53.33333,
"Lon" : "13.99999",
"SensorMaker" : "Hitachi",
"SensorClass" : "Thermometer",
"Dimension" : "SoilTemperature_0.05mSensor1",
"Gauge" : "degC"
}
And I would like to change the complete collection (~ 90k items) to this, to conform to minimal 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"
}
I tried to convert it using this query, but whatever I do I will receive an error the like "Line 5: Unexpected string":
db.sensor_geo.aggregate([
{ '$group' : {
'_id' : '$_id',
'Toponym' : '$Toponym'
'geometry': { 'type': 'Point', { $set : {"coordinates.$[]": [ {'$Lat', '$Lon'} ] }}},
'SensorMaker' : '$SensorMaker',
'SensorClass' : '$SensorClass',
'Dimension' : '$Dimension',
'Gauge' : '$Gauge'
}
}
]);
Should I've used $push
instead of $set
, even though this also lead nowhere? Do I also have to create an ObjectID for the nested Object, and that may have caused the problem?
回答1:
You can try below aggregation pipeline with bulk writes.
Below aggregation changes the Lat and Lon field to geometry with bulk update to write the new geometry field and remove the Lat and Lon fields.
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();
}
来源:https://stackoverflow.com/questions/48945082/convert-mongodb-schema-including-nested-object