阅读前提: 代码的背景是本ID其他的博文: ip库的查询
其格式如下:
begin end 省 市
18533376,18534399,内蒙古自治区,呼伦贝尔市
简单的代码:
public static IpLocation getLocation(long iplong) { IpLocation iplocation = new IpLocation(); // 获取表的句柄 DBCollection collection = null; // 封装查询条件 BasicDBObject basicDBObject = null; try { collection = db.getCollection(Constant.ipBase); basicDBObject = new BasicDBObject(); // begin <= iplong <= end basicDBObject.put("begin", new BasicDBObject("$lte", iplong)); basicDBObject.put("end", new BasicDBObject("$gte", iplong)); List<DBObject> dbObjectList = collection.find(basicDBObject) .toArray(); iplocation.setProvince(dbObjectList.get(0).get("province") .toString()); iplocation.setCity(dbObjectList.get(0).get("city").toString()); } catch (Exception e) { p("查MixBoxIpBase库的过程之中出现了错误"); } return iplocation; }
在这里
basicDBObject.put("begin", new BasicDBObject("$lte", iplong)); lte:小于等于表示 begin 《= iplong basicDBObject.put("end", new BasicDBObject("$gte", iplong)); get: 大于等于表示 end 》= iplong 综合起来来看就是 begin < iplong < end, 细节方面请不要出现纰漏~
时间: 2024-11-08 04:02:39