Reputation: 2194
I am looking for the mongo equivalent to this SQL Query:
SELECT country
FROM geoip
WHERE
2921648058 BETWEEN start AND end
My MongoDB Documents look like this:
{
"_id" : ObjectId("52daefed0c78fc8c9085498d"),
"country" : "DE",
"end" : "16777471",
"start" : "16777216"
}
The numbers are IP Adresses converted to int format. Unfortunately all searches regarding this only come up with the $gt and $lt operators, which aren't of much use in this case, because i don't want to retrieve documents with a certain field inside a range, but documents where a range provided the document itself fits a given number from the search.
Going through all the records and evaluating the result in a script is not an option because of the massive size of the database.
Upvotes: 0
Views: 1086
Reputation: 311865
The $gte
and $lte
operators do actually apply here; you just need to use separate operators for start
and end
:
ip = 2921648058;
db.geoip.find({'start': {'$lte': ip}, 'end': {'$gte': ip}})
Upvotes: 6