User:Jules/mongobasic: Difference between revisions

From XPUB & Lens-Based wiki
 
(3 intermediate revisions by the same user not shown)
Line 24: Line 24:
<small>''→ anything with longitude greater than 1 and lower than 12, latitude greater than 1 and lower than 3.1''</small><br />
<small>''→ anything with longitude greater than 1 and lower than 12, latitude greater than 1 and lower than 3.1''</small><br />
<small>''This is very useful because it enables to give a range...''</small>
<small>''This is very useful because it enables to give a range...''</small>
<span style="font-family:monospace;font-size:13px">db.ips.distinct("location.country_name").sort()</span><br />
<small>''→ will return all the different countries listed in the database and sort them alphabetically''</small>
<span style="font-family:monospace;font-size:13px">db.ips.distinct('coordinates.1',{'coordinates.0':3})</span><br />
<small>''→ All different latitudes possible for longitude: 3 ''</small><br />
<span style="font-family:monospace;font-size:13px">db.ips.find( { "coordinates" : { $near: [3, 48], $maxDistance: 7 / 111.2 } } );</span><br />
<small>''→ the query says it all ''</small><br />
<span style="font-family:monospace;font-size:13px">mongo databasename < script.js </span><br />
<small>''→ to move the query to a js file''</small>


==export jsons==
==export jsons==

Latest revision as of 09:34, 22 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...

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