User:Jules/mongobasic

From Media Design: Networked & Lens-Based wiki
Jump to navigation Jump to search

Import the Geoip database

I was lucky to find the MaxMind GeoIP2 legacy csv imported in MongoDB here:
http://www.poeticoding.com/downloads/mongodb/mongodb_geoips.zip
I unzipped the folder for the dump and used mongorestore to import it.

First steps

db.ips.find()
→ will find anything

db.ips.find().pretty()
→ for it to be readable

db.ips.find({'location.country_name' : /^antarctica$/i}).pretty()
→ Will find ips in antarctica and show them in a readable way (the ^ is here in case it's a lower or upper case first letter)

db.ips.find({'coordinates.1':12}).pretty()
→ anything with latitude of 12

db.ips.find({'coordinates.0':12}).pretty()
→ anything with longitude of 12

db.ips.find({ 'coordinates.0': {$gte: 1, $lte:12},'coordinates.1': {$gte:1, $lte:3.1} }).pretty()
→ anything with longitude greater than 1 and lower than 12, latitude greater than 1 and lower than 3.1
This is very useful because it enables to give a range...

db.ips.distinct("location.country_name").sort()
→ will return all the different countries listed in the database and sort them alphabetically

db.ips.distinct('coordinates.1',{'coordinates.0':3})
→ All different latitudes possible for longitude: 3

db.ips.find( { "coordinates" : { $near: [3, 48], $maxDistance: 7 / 111.2 } } );
→ the query says it all

mongo databasename < script.js
→ to move the query to a js file

export jsons

mongoexport -d poetic_geoips -c ips -q "{'location.country_name' : /^antarctica$/i}" --jsonArray --out antarctica.json
→ get a json file of all the ips in Antarctica

mongoexport -d poetic_geoips -c ips -q "{'location.city_name' : /^rotterdam$/i}" --jsonArray --out rotterdam.json
→ to get a json file of all the ips in Rotterdam

mongoexport -d poetic_geoips -c ips -q "{'coordinates.0':{\$gte:3.1,\$lte:12.3},'coordinates.1': {\$gte:1.9, \$lte:3.1}}" --jsonArray --out supersuper.json
→ SuperCombo