Does Postgis's ST_Within and Mongodb's $geoWithin perform the same way and should return the same result?

42 Views Asked by At

I have been trying to compare the Geospatial query function of two well-known database engine, Mongod and Postgis to see which one would suit my work the most.

I prepared mock data for my database, which is identical in both databases.

I tried playing around with the geofencing query to find number of items within a provided polygon coordinates.

But the returned results were not the same. Mongo could not find the Polygon type item within the provided area while Postgis can, and Mongo result were more than of Postgis's.

Was it my fault in the query code, or is there an underlying difference between the two function from each database?

The query from both of them were the following:

Query for Postgis

-- # For Postgis
SELECT 
    type
    , count(id)
FERE ST_Within(geojson, ST_GeomFromText('POLYGON((100.95725876372013 16.84256184570458,101.08808706513712 18.77654822388623,102.55103520605854 16.583603557822975,100.95725876372013 16.84256184570458))'))
GROUP BY type
# Here the return result
type        count
"POINT"     38990
"POLYGON"       1

Query for Mongodb

# for Mongodb
db.geometryRoot.aggregate([
    {
        $match: {
            geojson: {
                $geoWithin: {
                    $geometry: {
                        type: "Polygon",
                        coordinates: [[
                            [100.95725876372013, 16.84256184570458], 
                            [101.08808706513712, 18.77654822388623], 
                            [102.55103520605854, 16.583603557822975], 
                            [100.95725876372013, 16.84256184570458]
                        ]]
                    }
                }
            }
        }
    }
    , {
        $group: {
            _id: "$type",
            count: { $count: { } }
        }
    }
])
# Result from Mongodb
{
  _id: 'POINT',
  count: 39175
}
0

There are 0 best solutions below