Sekai Sekai - 5 months ago 36
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]);


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