標籤:io os java ar sp cti 代碼 on c
閱讀前提: 代碼的背景是本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 <= endbasicDBObject.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 《= iplongbasicDBObject.put("end", new BasicDBObject("$gte", iplong)); get: 大於等於表示 end 》= iplong 綜合起來來看就是 begin < iplong < end, 細節方面請不要出現紕漏~
MongoDB【條件查詢】 - 比較的條件