Skip to content

Instantly share code, notes, and snippets.

Forked from lobodin/
Last active August 29, 2015 13:59
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Geonames cities to mongoDB thing

#Generate cities data

  1. wget
  2. unzip
  3. node geonames.js cities15000.txt

The result is json array of cities in the following format:

	_id: <ID>
	name: <Name>,
	alternate_names: [<alternatename1>,…],
	coordinates: [<Latitude>, <Longitude>]

#Import to MongoDB

mongoimport -d db_name -c cities cities.json --jsonArray
var fs = require('fs')
, _ = require('underscore');
var filename = process.argv[2];
if (!filename) {
console.log('File not found');
var cities = fs.readFileSync(filename).toString().split("\n");
cities.pop(); // empty element
var processedCities =, function(city) {
var data = city.split('\t');
var names = data[3].split(',');
return {
_id: Number(data[0]),
name: data[1],
alternate_names:, function(name) {
return name.toLowerCase();
coordinates: [Number(data[4]), Number(data[5])]
fs.writeFileSync('cities.json', JSON.stringify(processedCities));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment