Last active
August 29, 2015 14:20
-
-
Save wilensky/1a130894f0e6e91db168 to your computer and use it in GitHub Desktop.
(MySQL > MongoDB) + (Latitude, Longitude > GeoJSON) data migration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SELECT name, country, region, latitude, longitude | |
INTO OUTFILE '/tmp/dump.csv' | |
FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' | |
FROM table | |
-- | |
-- mongoimport -d db_name -c collection_name -type csv -f name,country,region,latitude,longitude --drop /tmp/dump.csv | |
-- | |
/** -- Mongo shell script -- | |
db.coll.find({$query:{}, $orderBy:{country: -1}}).forEach(function (e) { | |
e.geo = { | |
type: "Point", | |
coordinates: [e.longitude, e.latitude] // Longitude goes first, Latitude range: [-90..90] | |
}; | |
db.coll.save(e); | |
print(e.name +' '+ e.country); // Just to understand what is happening | |
}); | |
**/ | |
/** -- Removing unecessary lat/lon fields, bec. they are already in GeoJSON object -- | |
db.coll.update({}, {$unset: {latitude:1, longitude:1}}, false, true) | |
**/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment