2017-05-31 103 views
3

我有文档记录集的MongoDB(3.4)在象下面这样: -

{ 
     "_id" : ObjectId("592d0c78555a7436b0883960"), 
     "userid" : 7, 
     "addresses" : [ 
     { 
      "apporx" : 50.0, 
      "loc" : [ 
       -73.98137109999999, 
       40.7476039 
      ] 
     }, 
     { 
      "apporx" : 15.0, 
      "loc" : [ 
       -73.982002, 
       40.74767 
      ]   
     }, 
     { 
      "apporx" :10.0, 
      "loc" : [ 
       -73.9819567, 
       40.7471609 
      ] 
     } 
    ] 
    } 
` 

I created index on this collection using below query :- 
`db.records.createIndex({'addresses.loc':1})` 

when i execute my below query :- 
`db.records.aggregate( 
     {$geoNear : { 
     near : [ -73.9815103, 40.7475731 ], 
     distanceField: "distance" 
    }}); 

这个结果让我距离你field.now可以给我解释一下我的文档中的哪一个地址阵列在此多元素存在。我如何确定哪个元素的结果是真的?

另一个问题: - 如果我对“addresses.apporx”的条件喜欢大于或等于那么有没有办法找到这种情况的位置?

回答

2

首先,我强烈建议您为您的集合创建一个“2dsphere”索引,如果您打算对现实世界坐标进行地理空间查询。

确保将其他指标,你可能一直在玩:

db.records.dropIndexes(); 
db.records.createIndex({ "addresses.loc": "2dsphere" }) 

为了做到你想要什么,先来看看稍加修改还包括includeLocs选项$geoNear

db.records.aggregate([ 
    { "$geoNear": { 
    "near": [ -73.9815103, 40.7475731 ], 
    "spherical": true, 
    "distanceField": "distance", 
    "includeLocs": "locs" 
    }} 
]) 

现在您看到的输出,看起来像这样:

{ 
     "_id" : ObjectId("592d0c78555a7436b0883960"), 
     "userid" : 7, 
     "addresses" : [ 
       { 
         "apporx" : 50, 
         "loc" : [ 
           -73.98137109999999, 
           40.7476039 
         ] 
       }, 
       { 
         "apporx" : 15, 
         "loc" : [ 
           -73.982002, 
           40.74767 
         ] 
       }, 
       { 
         "apporx" : 10, 
         "loc" : [ 
           -73.9819567, 
           40.7471609 
         ] 
       } 
     ], 
     "distance" : 0.0000019174641401278624, 
     "locs" : [ 
       -73.98137109999999, 
       40.7476039 
     ] 
} 

那么返回的不仅是到最近点的距离,而是“哪个”位置是匹配使用的。

所以,如果你想$filter原始数组返回最近的,那么你可以:

db.records.aggregate([ 
    { "$geoNear": { 
    "near": [ -73.9815103, 40.7475731 ], 
    "spherical": true, 
    "distanceField": "distance", 
    "includeLocs": "locs" 
    }}, 
    { "$addFields": { 
    "addresses": { 
     "$filter": { 
     "input": "$addresses", 
     "as": "address", 
     "cond": { "$eq": [ "$$address.loc", "$locs" ] } 
     } 
    } 
    }} 
]) 

而这仅仅是比赛返回数组:

{ 
     "_id" : ObjectId("592d0c78555a7436b0883960"), 
     "userid" : 7, 
     "addresses" : [ 
       { 
         "apporx" : 50, 
         "loc" : [ 
           -73.98137109999999, 
           40.7476039 
         ] 
       } 
     ], 
     "distance" : 0.0000019174641401278624, 
     "locs" : [ 
       -73.98137109999999, 
       40.7476039 
     ] 
} 
+0

谢谢you..My第一个问题解决了。但是我的另一个问题是: - 如果我对“addresses.apporx”的条件进行了更大或相等的处理,那么是否有任何方法可以找到这种情况的位置? – 5a01d01P

+0

@SandipKumar不知道你的意思,这可能是一个不同的问题。但是如果你的意思是*“比较距离场”*投影到文档中,那么就像上面那样,添加另一个流水线阶段来比较这两个值。用'$ project'和'$ redact'来玩这个,阅读'$ geoNear'手册页中的“distanceField”文档。 –

+0

@SandipKumar关于“距离”的那点。使用GeoJSON格式可能比传统的“数组”格式更好。原因是从“遗产”返回的“距离”是**弧度**,需要转换为公里。 GeoJSON坐标用于比较时,会始终以**米**返回结果。所以这可能会更符合“存储距离”。 –