数据库中文档存储格式:
需求:
给定一个矩形范围,查询“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)里面怎么进行查询呢?
如下图: