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