Question

Hi guys I have the method below for counting within polygons in a mongodb:

public function countWithinPolygon($polygon, $tags = array())
        {
            // var_dump($polygon);

            // var_dump($polygon->getPoints());exit();
            $query = array(
                'point' => array(
                    '$within' => array(
                        '$polygon' => $polygon->getPoints()->first()->toArray(true)
                    )
                )
            );

            if($tags)
            {
                $query['tags'] = array(
                    '$all' => $tags
                );
            }

            return parent::count($query);
        }

For some queries with small amounts of data it is just okay. On larger datasets containing 4000+ calls the execution time is truely pathetic and can take hours. On average it takes three hours to execute. Any ideas or hints on a better way to write this to save time and optimize this query?

Était-ce utile?

La solution

The issue was fixed by ensuring an index like so : db.polygon.ensureIndex({'GeoJSON.geometry':'2dsphere'});

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top