2014-04-24 24 views
0

我將所有點,線條和多邊形存儲在集合中。我可以檢索使用$ geoWithin給定的多邊形內所有的點和線串,如下所示:

db.BLR_all.find(
    { 
     "geom" : { 
      "$geoWithin" : { 
       "$geometry" : { 
        "type" : "Polygon", 
        "coordinates" : [ [ /** array of points **/ ] ] 
       } 
      } 
     } 
    }) 

如何修改上面的查詢在給定的多邊形只提取點類型?

謝謝

回答

1

你的查詢修改其實原因很簡單,只包括了以GeoJSON類型查詢過濾條件:

db.BLR_all.find(
    { 
     "geom" : { 
      "$geoWithin" : { 
       "$geometry" : { 
        "type" : "Polygon", 
        "coordinates" : [ [ /** array of points **/ ] ] 
       } 
      } 
     }, 
     "geom.type": "Point" 
    }) 

或特別對一些樣本數據,

{ 
    "_id" : ObjectId("534cb37acca5f311cc5c23cc"), 
    "loc" : { 
      "type" : "Polygon", 
      "coordinates" : [ 
        [ 
          [ 
            0, 
            0 
          ], 
          [ 
            3, 
            6 
          ], 
          [ 
            6, 
            1 
          ], 
          [ 
            0, 
            0 
          ] 
        ] 
      ] 
    } 
} 
{ 
    "_id" : ObjectId("535aeddfb6b8fd04b3424a09"), 
    "loc" : { 
      "type" : "Point", 
      "coordinates" : [ 
        3, 
        6 
      ] 
    } 
} 
{ 
    "_id" : ObjectId("535aef22b6b8fd04b3424a0a"), 
    "loc" : { 
      "type" : "Point", 
      "coordinates" : [ 
        9, 
        12 
      ] 
    } 
} 

正在發送查詢:

db.geo.find({ 
    "loc": { 
     "$geoWithin": { 
      "$geometry": { 
       "type": "Polygon", 
       "coordinates": [ [ [ 0, 0 ], [ 3, 6 ], [ 6, 1 ], [ 0, 0 ] ] ] 
      } 
     } 
    }, 
    "loc.type": "Point" 
}) 

只返回屬於「點」且位於座標範圍內的項目。

{ 
    "_id" : ObjectId("535aeddfb6b8fd04b3424a09"), 
    "loc" : { 
      "type" : "Point", 
      "coordinates" : [ 
        3, 
        6 
      ] 
    } 
}