Aysennoussi
Aysennoussi

Reputation: 3860

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)
    {
        $this->setType($type);
        $this->setCoordinates($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 !

Edit! This is How I store it in PHP :

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

Upvotes: 1

Views: 759

Answers (1)

Aysennoussi
Aysennoussi

Reputation: 3860

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

Upvotes: 1

Related Questions