您的位置:首页 > 数据库 > Mongodb

$within进行空间查询--MongoDB API for Java

2014-07-21 11:13 627 查看
数据库中文档存储格式:



需求:

给定一个矩形范围,查询“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)里面怎么进行查询呢?

如下图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: