Search code examples
mongodbgoogle-mapsgoogle-maps-api-3twitter

Query only documents that have values in it in MongoDB


I wanted to map the coordinates of the tweets that im collecting from Twitter Streaming API. While I was collection data from Twitter, I noticed that some documents dont have "coordinates". I've done db.tweets.ensureIndex({"coordinates.coordinates":"2d"}) in the mongodb shell to make things faster. My query in PHP is this db.tweets.find({},{"_id":0, "coordinates.coordinates": 1});. However when I reload my website, it doesnt create a marker. My code seems to be working if all of the documents in MongoDB contain coordinates. But if there are documents that dont have coordinates, no marker will be shown in my website even if there are documents that have coordinates. Is there a way to project only documents that have the "coordinates"?

Here is my code

<!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>

When I query in mongodb shell using db.tweets.find({},{"_id":0, "coordinates.coordinates": 1}); (assorted documents with and without coordinates), this is the output:

{  }
{  }
{  }
{ "coordinates" : { "coordinates" : [ -74.0064, 40.7142 ] } }
{  }
{ "coordinates" : { "coordinates" : [ 121.9197351, 11.96795331 ] } }

Is there a way to project only these?

{ "coordinates" : { "coordinates" : [ -74.0064, 40.7142 ] } }
{ "coordinates" : { "coordinates" : [ 121.9197351, 11.96795331 ] } }

UPDATE

I found a way to return only those fields with existing values using

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

Now im trying to figure out how to convert this query inside the PHP. I seem to get an error in $ne when i use this query db.tweets.find( [ "coordinates" => [ $ne => null ] ], [ "_id"=>0, "coordinates.coordinates"=> 1] )


Solution

  • you can use $exists like this:

    db.tweets.find(
      {"coordinates": {$exists: true}},
      {"_id":0, "coordinates.coordinates": 1}
    );