數據庫中文檔存儲格式:
需求:
給定一個矩形範圍,查詢“bound_box”中“coordinates”的值落在此矩形範圍內的文檔
解決方案:
db=conn.getDB("testgps");
DBCollection users=db.getCollection("ST_Layer_ST_Layer_gpslayer1");
List<Double[]> polygon = new ArrayList<Double[]>();
polygon.add(new Double[]{0.0,0.0});
polygon.add(new Double[]
{ 0.0, 25.2 });
polygon.add(new Double[]
{ 117.3, 25.2 });
polygon.add(new Double[]
{ 117.3, 0.0 });
DBObject searchObj = new BasicDBObject("$within", new BasicDBObject("$polygon",polygon));
DBObject output=new BasicDBObject("bound_box.coordinates",searchObj);
DBCursor cursor=users.find(output);
while(cursor.hasNext())
{
//DBObject Obj=(DBObject)cursor.next();
//DBObject Obj1=Obj.get("start_time");
//Date dt=(Date)Obj.get("start_time");
System.out.println(cursor.next());
}
自己測試成功。
在命令行提示符(shell)裏面怎麼進行查詢呢?
如下圖: