User:Jules/mongobasic: Difference between revisions

From XPUB & Lens-Based wiki
No edit summary
Line 31: Line 31:
<span style="font-family:monospace;font-size:13px">mongoexport -d poetic_geoips -c ips -q "{'location.city_name' : /^rotterdam$/i}" --jsonArray --out rotterdam.json</span><br />
<span style="font-family:monospace;font-size:13px">mongoexport -d poetic_geoips -c ips -q "{'location.city_name' : /^rotterdam$/i}" --jsonArray --out rotterdam.json</span><br />
<small>''→ to get a json file of all the ips in Rotterdam''</small>
<small>''→ to get a json file of all the ips in Rotterdam''</small>
<span style="font-family:monospace;font-size:13px">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 </span><br />
<small>''→ SuperCombo''</small>

Revision as of 19:55, 20 November 2015

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...

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