2017-02-04 87 views
1

我有下面的文档集合

{ 
"_id" : ObjectId("589550ae930b4acade1e2157"), 
"itemId" : "2", 
"loc" : { 
    "type" : "GeometryCollection", 
    "geometries" : [ 
     { 
      "type" : "Point", 
      "coordinates" : [ 
       -121.9771985, 
       37.5637936 
      ], 
      "address" : "Athy St" 
     }, 
     { 
      "type" : "Point", 
      "coordinates" : [ 
       -121.9430202, 
       37.3972994 
      ], 
      "address" : "River Side Ct" 
     } 
    ] 
} 

我使用下面的查询来查找距离最近的位置上的物品中:

db.store.find(
    { 
    loc: 
     { $near : 
      { 
      $geometry: { type: "Point", coordinates: [-121.9777269,37.5607686] }, 
      $maxDistance: 3.0 * 1609.34 
      } 
     } 
    } 
) 

我我试图限制只读取和显示最接近的匹配地址。假设被查询的地点只与River Side ct匹配,我不想返回Athy Street。这是几何集合数组。我试图使用预测,但没有奏效。

你能告诉我如何限制吗?任何帮助真的很感激。

在此先感谢。

马迪

回答

1

MongoDB的$near操作各种按距离(最接近最远, 如documentation提到的),所以你必须做的是限制的结果。

db.store.find(
    { 
    loc: 
     { $near : 
      { 
      $geometry: { type: "Point", coordinates: [-121.9777269,37.5607686] }, 
      $maxDistance: 3.0 * 1609.34 
      } 
     } 
    } 
).limit(1)