2017-05-31 104 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座標用於比較時,會始終以**米**返回結果。所以這可能會更符合「存儲距離」。 –