1

I've imported http://www.maxmind.com/en/city (GeoIP City) into MongoDB. There are two collections: blocks and location.

Blocks have start of ip range and end of an ip range and an integer (will make this a MongoID later) indicating a document in the location collection.

ip addresses in are stored as NumberLong in the blocks collection.

I get an ip address and convert it into long with the php function iptolong (http://php.net/manual/en/function.ip2long.php), giving me a value like 3758096128

Now I need to make a query that get me the id from the correct document in the blocks collection and then look up the location in the location collection (with the help of the id)

I can obvious make two queries, but this is not very efficient.

My first step would be something like (in PHP):

$query = array(
    'startIpNum' => array('$gte' => $ipLong),
    'endIpNum' => array('$lte' => $ipLong),
    );

(Still figuring out why above query does not return any data)

But what would be the next step in the process in getting data from two collections?

Marc Witteveen
  • 743
  • 2
  • 10
  • 25
  • You need to do a JOIN query on the collection. Take a look at this example: http://stackoverflow.com/a/4588699/2332336 – Latheesan Nov 04 '13 at 11:12
  • Thanks you might be right! Any idea why above statement does not return any data? What I am trying to do is (in SQL) SELECT * FROM blocks WHERE startIpNum => $ipLong AND endIpNum <= $ipLong; – Marc Witteveen Nov 04 '13 at 11:51
  • 1
    Try the same query from the MongoDb console for you database and collection. What are the results? – WiredPrairie Nov 04 '13 at 11:59
  • I read a bit more about the query and it seems I need to be using $and $query = array( '$and' => array( 'startIpNum' => array('$gte' => $ipLong), 'endIpNum' => array('$lte' => $ipLong), ) ); Which gives me an error: $or requires nonempty array Had this before with $set because it was between double quotes, bit replacing them with one quote does not do the trick here. Any suggestions? – Marc Witteveen Nov 06 '13 at 13:16

0 Answers0