Skip to content

Instantly share code, notes, and snippets.

@kejyun
Created September 18, 2013 03:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save kejyun/6604139 to your computer and use it in GitHub Desktop.
Save kejyun/6604139 to your computer and use it in GitHub Desktop.
MongoDB Aggregation LBS查詢:找捷運台北車站附近距離2公里內的捷運站、依照建立時間做排序
// 找捷運台北車站附近距離2公里內的捷運站、依照建立時間做排序
db.lbs_test.aggregate(
{
$geoNear : {
near : [25.046084,121.517542],
distanceField : "dist",
maxDistance : 2 / 111.12
}
},
{
$sort : {
time : 1
}
}
);
// 查詢結果
{
"result" : [
{
"_id" : ObjectId("5238179cdfba7ac818bb5fcd"),
"name" : "捷運臺大醫院站",
"loc" : {
"lat" : 25.041535,
"long" : 121.516029
},
"time" : 11,
"dist" : 0.004794013975784349
},
{
"_id" : ObjectId("5238179cdfba7ac818bb5fce"),
"name" : "捷運中正紀念堂站",
"loc" : {
"lat" : 25.032922,
"long" : 121.518481
},
"time" : 12,
"dist" : 0.01319545243635133
},
{
"_id" : ObjectId("5238179cdfba7ac818bb5fd2"),
"name" : "捷運中山站",
"loc" : {
"lat" : 25.052596,
"long" : 121.520327
},
"time" : 16,
"dist" : 0.0070825397280881355
},
{
"_id" : ObjectId("5238179cdfba7ac818bb5fd3"),
"name" : "捷運雙連站",
"loc" : {
"lat" : 25.057961,
"long" : 121.520842
},
"time" : 17,
"dist" : 0.012326926989316265
},
{
"_id" : ObjectId("5238179ddfba7ac818bb5fd4"),
"name" : "捷運民權西路站",
"loc" : {
"lat" : 25.063093,
"long" : 121.519425
},
"time" : 18,
"dist" : 0.017112912376328723
},
{
"_id" : ObjectId("523817b1dfba7ac818bb5fdc"),
"name" : "捷運松江南京站",
"loc" : {
"lat" : 25.051974,
"long" : 121.53303
},
"time" : 26,
"dist" : 0.016570161254487518
},
{
"_id" : ObjectId("523817b1dfba7ac818bb5fdd"),
"name" : "捷運東門站",
"loc" : {
"lat" : 25.033972,
"long" : 121.528695
},
"time" : 27,
"dist" : 0.01646480953427314
}
],
"ok" : 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment