import data
sudo mongoimport --collection countries --file ./data/countries.json
sudo mongoimport --collection pcode --file ./data/geoInfos.json
countries
country: String,
country_name: String,
currency_code: String,
currency_symbol: String,
address: [String],
states: [String]
pcode
country: String,
pcode: String
maxmind DB GeoLite2 Country http://geolite.maxmind.com/download/geoip/database/GeoLite2-Country.mmdb.gz
location: ./data/GeoLite2-Country.mmdb
pincode data:
{ success: true,
data:
{ admin_level_1: 'West Bengal',
admin_level_2: 'Bardhaman',
country: 'IN',
pcode: '713148' } }
ip and country data:
{ success: true,
data:
{ country: 'DE',
country_name: 'Germany',
currency_code: 'EUR',
exchange_rate: 0.014500824653516325,
currency_symbol: '€',
states:
[ 'Brandenburg',
'Berlin',
'Baden-Württemberg',
'Saarland',
'Sachsen',
'Sachsen-Anhalt',
'Thüringen' ] } }
country list data:
{ success: true,
data:
[ { country: 'SG', country_name: 'Singapore' },
{ country: 'HK', country_name: 'Hong Kong' },
{ country: 'GR', country_name: 'Greece' },
{ country: 'KZ', country_name: 'Kazakhstan' }] }
no_data
{ success: false,
data: null }
in example collection name is geoInfos. more appropriate name - pcodes. module doesn't care. since you are providing the collection object.
download and extract from http://download.geonames.org/export/zip/allCountries.zip
import from allCountries.zip file.
mongoimport --db letzchange --collection geoInfos --type tsv --fields country,pcode,area,admin_level_1,admin_code_1,admin_level_2,admin_code_2 --file ./allCountries.txt
unset unnecessary fields.
db.geoInfos.update(
{ },
{ $unset: { area:"", admin_code_1: "", admin_code_2: "", field7: "", field8: "",field9: "",field10: "",field11: "", } },
{ multi: true }
)
normalize data. at this point some pincodes are string and others are numbers.
db.geoInfos.find({pcode : {$exists : true}}).forEach( function(obj) { obj.pcode = ""+obj.pcode; db.geoInfos.save(obj); } );
now all pincodes are string.
then create index on the pcode and country fields with unique and drop_duplicates set to true. this removes duplicate data. brings down count from 10 lakhs to 6 lakhs.
import from data/countries.json
check out test.js
find_by_ip (country_collection, maxmindDB, _ip, _cb)
country_list (country_collection, _cb)
find_by_country (country_collection, _country, _cb)
pincode_find (pcode_collection, _country, _pcode, _cb)
update_exchange_rates (oxr_app_id, _cb)
var MongoClient = require('mongodb').MongoClient;
var x = require("./module_geo");
var url = "mongodb://cloud9:cloud9@kahana.mongohq.com:10099/doba";
var oxr_app_id = "7b422add4315455088117851ff9157c4";
var db, country, pcode;
var maxminddb = require('maxmind-db-reader').openSync( require("path").join(__dirname + '/data/GeoLite2-Country.mmdb'));
MongoClient.connect(url, function(err, _db) {
if(err) return console.log(err);
db = _db;
country = db.collection('countries');
pcode = db.collection('pcode');
//update exchange rates at least once on startup.
x.update_exchange_rates(oxr_app_id, function() {
if(!err){
run_test();
//setInterval(run_test, 5*1000);
}
});
});
function run_test() {
console.log("..");
x.country_list(country, custom_cb);
x.find_by_country(country, "GR", custom_cb);
x.find_by_ip(country, maxminddb, "5.5.5.5",custom_cb);
x.pincode_find(pcode, "IN","713148",custom_cb);
x.update_exchange_rates(oxr_app_id, null);
}
function custom_cb(err,result) {
console.log(err, result);
}
data from http://www.geonames.org and GeoLite2 data created by MaxMind, available from http://www.maxmind.com
npm install --save module_geo
all callbacks are invoked with params (err, result)
err
is set if there is application level error.
result.success
is false if there is data level error.
module is malfunctioning if err !== null
.
else the result can be piped to client.
var geo = require("module_geo");
geo.init("mongodb://cloud9:cloud9@kahana.mongohq.com:10099/doba", "7b422add4315455088117851ff9157c4");
geo.country_list(console.log);
geo.find_by_ip("5.5.5.5",console.log);
geo.find_by_country("IN",console.log);
geo.pincode_find("IN","713148",console.log);
geo.update_exchange_rates();