Click Below to subscribe

How to find all points that lies within a polygon in mongoose 2022

In this article, we gonna learn how to find all points that lies within a polygon.

We need to use GeoJSON objects for geospatial queries.

Ref:- https://mongoosejs.com/docs/geojson.html

Point Schema:- The most simple structure in GeoJSON is a point. 

{
    "type" : "Point",
    "coordinates" : [-122.5, 37.7]
}

Note:- that longitude comes first in a GeoJSON coordinate array, not latitude.

models/user.model.js

const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const userSchema = new Schema(
    {
        name: String,
        location: {
            type: {
                type: String,
                enum: ['Point'],
                required: true
            },
            coordinates: {
                type: [Number],
                required: true
            }
        }
    },
    {
        timestamps: true
    }
);

userSchema.index({ location: '2dsphere' }); 

const User = mongoose.model('User', userSchema);

module.exports = User;

Insert Dummy Data

await User.insertMany([
    {
        name: 'User 1',
        location: {
            type: 'Point',
            coordinates: [79.5636015, 28.6453172]
        }
    },
    {
        name: 'User 2',
        location: {
            type: 'Point',
            coordinates: [79.8552963, 28.6547708]
        }
    },
    {
        name: 'User 3',
        location: {
            type: 'Point',
            coordinates: [79.7687766, 28.6354746]
        }
    },
    {
        name: 'User 4',
        location: {
            type: 'Point',
            coordinates: [79.8443076, 28.6185988]
        }
    },
    {
        name: 'User 5',
        location: {
            type: 'Point',
            coordinates: [79.7674028, 28.7089722]
        }
    } 
]);

Here is an example for finding all points that lies within a polygon.

 

Example using find()

const region = {
    type: 'Polygon',
    coordinates: [[
        [79.7613606, 28.7187879], 
        [79.6954426, 28.6055167], 
        [79.8918232, 28.5633101], 
        [79.9316487, 28.6934932], 
        [79.7613606, 28.7187879]
    ]]
};

const users = await User.find({
    location: {
        $geoWithin: {
            $geometry: region
        }
    }
});

 

Example using static function

We can also make it reusable by defining this as a static function inside the model.

userSchema.static('findPoint', function (region) {
    return this.find({
        location: {
            $geoWithin: {
                $geometry: region
            }
        }
    });
});

Now in the controller, you can call this function.

const users = await User.findPoint(region);

 

Example using aggregate()

const users = await User.aggregate([
    {
        $match: {
            location: {
                $geoWithin: {
                    $geometry: region
                }
            }
        }
    }
]);

Checkout my full mongoose-geowithin example.

https://github.com/ultimateakash/mongoose-geowithin

Leave Your Comment