Pergunta

i was reading at that tutorial to see how get documents that has position inside a polygon given by google maps polygon drawer.

In that tutorial it stores the position in an array like:

pos:{lat:xxx,lng:yyy}

That is not my case because in my documents that value are stored like:

{...
"hotel_lat":"xxx",
"hotel_lng":"yyy",
...
},
{...
"hotel_lat":"xxx",
"hotel_lng":"yyy",
...
},
{...
"hotel_lat":"xxx",
"hotel_lng":"yyy",
...
},
...

So in the tutorial it uses that query to find all documents in polygon:

$query = array("pos" => array(
               '$within' => array('$polygon' => $poly)));
$cursor = $collection->find($query);

I change it in:

$query = array(
array("hotel_lat","hotel_lng") => array(
'$within' => array('$polygon' => $poly)))

but it gives me that error: Illegal offset type in.....

What could be the error?

Thanks!

Foi útil?

Solução

Before hunting down further tutorials, which I gather you have done in this case because you wanted something written with PHP, the MongoDB site has the basic tutorial information that you need:

http://docs.mongodb.org/manual/tutorial/query-a-2d-index/

And before you say, "This is not PHP" then look at the code below:

$result = '{"$pos": { "$within": { "$polygon": "$poly" } } }';
print_r( json_decode( $result ) );

With the output

stdClass Object
(
    [$pos] => stdClass Object
        (
            [$within] => stdClass Object
                (
                    [$polygon] => $poly
                )

        )

)

So that gives you an idea of how to write this in PHP. And the reverse is true to convert to JSON and see if your coded form is right.

So the code you have actually written, isn't even a valid structure for PHP, let alone being way off the required syntax.

In the core documentation it is explained what your storage must look like, and that the preferred form in MongoDB storage is the array form, in order to consistently maintain the order across languages that may not be able to maintain the required order of a hash:

pos : [ <longitude> , <latitude> ]

You cannot have two separate fields for the "longitude" and "latitude" values, as MongoDB cannot create the required Geo-spatial index over multiple fields. Should you have managed to receive your data where the values where in discreet fields, then it is up to you to re-shape that data for the correct storage requirements for MongoDB.

Please read through all the MongoDB documentation first, before moving on to other tutorial tasks. Fix your data so that it is in a correct format, and index the required field. Then you can work with Geo-spatial queries.

And please use json_encode and json_decode regularly during your development process, as it is a very clear way to compare to documentation samples and visualize that you are doing things right:

$poly = array();
array_push(
  $poly, floatval(51.52081391707402), floatval(-0.12737274169921875),
  floatval(51.487582985843034), floatval(-0.13578414916992188));

$test = array("pos" => array('$within' => array('$polygon' => $poly)));
echo json_encode( $test, JSON_PRETTY_PRINT ) ."\n"

Resulting in:

{
    "pos": {
        "$within": {
            "$polygon": [
                51.520813917074,
                -0.12737274169922,
                51.487582985843,
                -0.13578414916992
            ]
        }
    }
}

As a JSON representation of how the structure is actually transmitted to the server converted as BSON.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top