$within進行空間查詢--MongoDB API for Java

數據庫中文檔存儲格式:



需求:

給定一個矩形範圍,查詢“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)裏面怎麼進行查詢呢?

如下圖:


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章