I have a collection, called Cities with city info in it. Each city document has an inner IpIntervals array ({StartNum, EndNum}) that contians IpIntervals for the city. Each interval boundary is calculated using formula 256 * 256 * 256 * a + 256 * 256 * b + 256 * c + d where "a.b.c.d" is ip address. To find location by ip address I'm using query:
{IpIntervals: $elemMatch: {"StartNum": {$lte: <<my_ip_num>>}, "EndNum": {$gte: <<my_ip_num>>}}}}
which works great but it takes about 270 ms, so I want to use some index with it. I've tried different indexes like:
{"IpIntervals.StartNum": 1, "IpIntervals.EndNum": 1}
, {"IpIntervals.StartNum": -1, "IpIntervals.EndNum": 1}
, {"IpIntervals.StartNum": 1, "IpIntervals.EndNum": -1}
, {"IpIntervals.StartNum": 1}
But nothing seems to work: it is always BasicCursor and 270ms, which is not good. Any ideas about what index is appropriate in this situation? Thanx.
Sample data:
{
"_id" : { "$oid" : "51015e8bd246e8e455ee027d" },
"Name" : "SomeCity",
"Latitude" : 28.755787,
"Longitude" : 37.617634,
"IpIntervals" : [
{ "StartNum" : 2457360384, "EndNum" : 2457360639 },
{ "StartNum" : 2457361408, "EndNum" : 2457362431 },
{ "StartNum" : 2457364480, "EndNum" : 2457366527 },
{ "StartNum" : 2461648896, "EndNum" : 2461650943 }
]
}