質問

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.

役に立ちましたか?

解決

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}})
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top