我想映射我从推特流API收集的推特的坐标。当我从推特上收集数据时,我注意到一些文档没有“坐标”。我已经做了
db.tweets.ensureIndex({"coordinates.coordinates":"2d"})
在mongodb shell中,让事情变得更快。我在PHP中的查询如下
db.tweets.find({},{"_id":0, "coordinates.coordinates": 1});
. 然而,当我重新加载我的网站时,它不会创建标记。如果MongoDB中的所有文档都包含坐标,那么我的代码似乎可以正常工作。但是如果有没有坐标的文档,即使有有坐标的文档,也不会在我的网站上显示任何标记。有没有办法只投影具有“坐标”的文档?
这是我的密码
<!DOCTYPE html>
<html>
<head>
<style>
#map {
height: 700px;
width: 100%;
}
</style>
</head>
<body>
<?php
include "connection.php";
session_start();
?>
<?php
$document = $collection->find([],['_id' => 0,'coordinates.coordinates' => 1]);
$json = array();
foreach($document as $row)
{
$json[] = $row;
}
?>
<script type="text/javascript">
var json = <?php echo json_encode($json);?>;
</script>
<h3>My Google Maps Demo</h3>
<div id="map"></div>
<script>
function initMap()
{
var center = {lat: 12.8797, lng: 121.7740};
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 6,
center: center,
// minZoom: 6
});
for (var i = 0, length = json.length; i < length; i++) {
var data = json[i],
latLng = new google.maps.LatLng(data.coordinates.coordinates[1], data.coordinates.coordinates[0]);
// Creating a marker and putting it on the map
var marker = new google.maps.Marker({
position: latLng,
map: map,
title: data.title
});
}
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=mykey&callback=initMap">
</script>
</body>
</html>
当我在mongodb shell中使用
数据库。推特。查找({},{“\u id”:0,“坐标。坐标”:1});
(有坐标和无坐标的分类文档),这是输出:
{ }
{ }
{ }
{ "coordinates" : { "coordinates" : [ -74.0064, 40.7142 ] } }
{ }
{ "coordinates" : { "coordinates" : [ 121.9197351, 11.96795331 ] } }
有没有办法只投射这些?
{ "coordinates" : { "coordinates" : [ -74.0064, 40.7142 ] } }
{ "coordinates" : { "coordinates" : [ 121.9197351, 11.96795331 ] } }
更新
我找到了一种方法,使用
db.tweets.find( { "coordinates" : { $ne: null } }, { "_id":0, "coordinates.coordinates": 1} );
.
现在,我想知道如何在PHP中转换此查询。我好像有个错误
$ne
使用此查询时
db.tweets.find( [ "coordinates" => [ $ne => null ] ], [ "_id"=>0, "coordinates.coordinates"=> 1] )