5.2 Coordinate conversion

When exporting WordPress data to MongoDB, no values were entered for the geometry field, only for the coordinates field. In this way, the geometry field needs to be fed with data from coordinates field of its own document.

The process of assigning value for geometry field was done in two steps:

First, for all documents where the coordinates field has value, was assigned a GeoJSON object to the geometry field, with type MultiPoint and coordinates of the coordinates field, using the following command:

db.pages.find({
      "coordinates": {
          "$gt": []
      }
}).forEach(function(data) {
  var geometria = {
      type: "MultiPoint",
      coordinates: data.coordinates
  };
  data.geometry = geometria;
  db.pages.save(data);
});

Next, for all documents where the coordinates field has only one pair of coordinates, was assigned a GeoJSON object to the geometry field, with type Point and coordinates of the coordinates field, using the following command:

db.pages.find({
      "coordinates": {
          "$size": 1
      }
}).forEach(function(data) {
  var geometria = {
      type: "Point",
      coordinates: data.coordinates[0]
  };
  data.geometry = geometria;
  db.pages.save(data);
});

5. Geospatial queries