Sekai Sekai - 1 year ago 123
PHP Question

How do I store GeoJson in MongoDB with PHP

I'm trying to store a location in a GeoJson format in MongoDb using Doctrine ODM:
What I do is :

The mapping:

class Coordinates

/** @MongoDB\String */
protected $type;

/** @MongoDB\Hash */
protected $coordinates;

public function __construct($type='Point',$coordinates)

When I try to store one location I get it as follow :

coordinates: { type: "Point", coordinates: { 0: -6.855243444442749, 1: 33.9704221689807 } }

which is not the GeoJson format.
The correct format should be

coordinates: { type: "Point", coordinates: [ -6.855243444442749,33.9704221689807 ] }

How Can I achieve this ?
Thank you very much !

This is How I store it in PHP :

$coordinates=new Coordinates('Point',[$lng,$lat]);

Answer Source

Yes ! I got my answer on twitter :P I should have mapped the whole coordinates field as a Hash

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download