1

我目前正在運行一個MongoDB實例來實時保存收集的推文在地理框中。因此,我想生成一個熱圖來顯示阿姆斯特丹發送的最多推文的位置。爲此,我必須僅查詢地理線路。這適用於以下代碼行:格式MongoDB查詢輸出

db.testtweets.find({"geo": { "$ne": null } }, { "geo": 1 }); 

不幸的是,這會返回更多信息,然後Google Maps API需要。輸出:

{ "_id" : ObjectId("56fea2cf206e3712f3d1a9bb"), "geo" : { "type" : "Point", "coordinates" : [ 52.3746373, 4.85773855 ] } } 

我想作爲輸出:

52.3746373, 4.85773855 

我是很新的MongoDB的,所以會很感激的任何建議。

回答

1

你可以開始使用find()最接近的是:

db.testtweets.find(
    { "geo": { "$ne": null } }, 
    { "geo.coordinates": 1, "_id": 0 } 
) 

主要生產:

{ "geo" : { "coordinates" : [ 52.3746373, 4.85773855 ] } } 

從那裏,你使用的客戶端處理返回的「座標」排列字段值。


您也可以使用aggregate()方法來做到這一點。你所需要的只是$project你的文件。

db.testtweets.aggregate([ 
    { "$match": { "geo": { "$ne": null } } }, 
    { "$project": { 
     "coordinates": "$geo.coordinates", 
     "_id": 0 
    }} 
]); 

這將產生類似:在PHP

{ "coordinates" : [ 52.3746373, 4.85773855 ] } 

翻譯給出:

db.testtweets.aggregate(array( 
    array("$match" => array("geo" => array("$ne" => null)), 
    array("$project" => array( 
     "coordinates" => "$geo.coordinates", 
     "_id" => 0 
    )) 
)); 
+0

謝謝,這確實給了你所提到的輸出。有什麼辦法只輸出數字52.3746373,4.85773855? – Joost