2017-03-06 79 views
0

我有一個簡單的文件,其具有在一個陣列3級的位置的對象。獲取最接近的位置中的數組

數據:

{ 
    "_id" : ObjectId("57c3c479a306b3613cf1ee5b"), 
    "location_history" : [ 
     { 
      "location_name" : "Area 1", 
      "date" : 1472447609, 
      "_id" : ObjectId("57c3c479ac5a69612f0e0899"), 
      "location" : [ 
       24.7, 67.1790576 
      ] 
     }, 
     { 
      "location_name" : "Area 2", 
      "date" : 1472448059, 
      "_id" : ObjectId("57c3c63bac5a69612f0e089c"), 
      "location" : [ 
       24.9663937, 67.1462044 
      ] 
     }, 
     { 
      "location_name" : "Area 3", 
      "date" : 1472448987, 
      "_id" : ObjectId("57c3c9dbac5a69612f0e08a0"), 
      "location" : [ 
       -24.987325, 115.1862298 
      ] 
     } 
} 

問:我需要獲取該數組中最接近的位置。

查詢我曾嘗試:

db.getCollection('consumers_locations').aggregate([ 
    {"$unwind": "$location_history"}, 
    {"$match":{"_id":ObjectId("57c3c479a306b3613cf1ee5b")}}, 
    {"$project" : { "abc" : "$location_history.location"} }, 
    { $geoNear: { 
     near: { type: "Point", coordinates: [ 24.942785, 67.157855 ] }, 
     distanceField: "distance", 
     query : {"_id" : "_id"}, 
     uniqueDocs: true, 
     includeLocs: "search_history.location", 
     maxDistance : 10000 
     } 
    } 
]) 

但我得到一個錯誤:

"ok" : 0, 
"errmsg" : "$geoNear is only valid as the first stage in a pipeline.", 
"code" : 2, 
"codeName" : "BadValue" 

預期輸出:

{ 
    "_id" : ObjectId("57c3c479a306b3613cf1ee5b"), 
    "location_history" : [ 
     { 
      "location_name" : "Area 1", 
      "date" : 1472447609, 
      "_id" : ObjectId("57c3c479ac5a69612f0e0899"), 
      "location" : [ 
       24.7, 67.1790576 
      ] 
     }, 
     { 
      "location_name" : "Area 2", 
      "date" : 1472448059, 
      "_id" : ObjectId("57c3c63bac5a69612f0e089c"), 
      "location" : [ 
       24.9663937, 67.1462044 
      ] 
     } 
} 

回答

0

它不適合你的模式。索引用於對集合中的文檔進行排序,而不是對文檔中的子文檔進行排序。

考慮創建一個單獨的location_history收集與在consumers_locations父文檔引用。例如。爲對象,收集可能看起來像:

db.getCollection('location_history').insert([ 
     { 
      "consumer_location": ObjectId("57c3c479a306b3613cf1ee5b"), 
      "location_name" : "Area 1", 
      "date" : 1472447609, 
      "_id" : ObjectId("57c3c479ac5a69612f0e0899"), 
      "location" : [ 
       24.7, 67.1790576 
      ] 
     }, 
     { 
      "consumer_location": ObjectId("57c3c479a306b3613cf1ee5b"), 
      "location_name" : "Area 2", 
      "date" : 1472448059, 
      "_id" : ObjectId("57c3c63bac5a69612f0e089c"), 
      "location" : [ 
       24.9663937, 67.1462044 
      ] 
     }, 
     { 
      "consumer_location": ObjectId("57c3c479a306b3613cf1ee5b"), 
      "location_name" : "Area 3", 
      "date" : 1472448987, 
      "_id" : ObjectId("57c3c9dbac5a69612f0e08a0"), 
      "location" : [ 
       -24.987325, 115.1862298 
      ] 
     } 
]); 

關於到錯誤,the docs閱讀:

You can only use $geoNear as the first stage of a pipeline.

因爲只有第一階段從索引中受益。

+0

變遷的模式將是一個很大的變化,我一直是作爲最後一件事我會做。沒有其他方法嗎?像「存儲的JavaScript」或其他東西? – Shaharyar

+0

但它會使數據的冗餘@Alex Blex –