Skip to content

Instantly share code, notes, and snippets.

@wilensky
Last active August 29, 2015 14:20
Show Gist options
  • Save wilensky/1a130894f0e6e91db168 to your computer and use it in GitHub Desktop.
Save wilensky/1a130894f0e6e91db168 to your computer and use it in GitHub Desktop.
(MySQL > MongoDB) + (Latitude, Longitude > GeoJSON) data migration
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