0

I want a list of Lawyers nearby, I have used MongoDB aggregation but it gives results only if I pass maxDistance greater than 100km. if I want to set maxDistance = 10 * 1000; then it is not giving any result but there are data available in the database.

this is model :

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

const lawyerSchema = new mongoose.Schema({
    name: {
        type: String,
        required: true
    },
    email: {
        type: String,
        required: true
    },
    password: {
        type: String,
        required: true
    },
    location: {
        type: { type: String, default: 'Point' },
        coordinates: { type: [Number], default: [0, 0] }
    }
})

lawyerSchema.index({ "location": "2dsphere" });

const Lawyer = mongoose.model('Lawyer', lawyerSchema)
module.exports = Lawyer

This is route :

//route for find nearby lawyer
router.get("/findLawyer", (req, res) => {
  const long = parseInt(req.query.long);
  const lat = parseInt(req.query.lat);

  Lawyer.aggregate(
    [
      {
        $geoNear: {
          near: {
            type: "Point",
            coordinates: [long, lat],
          },
          distanceField: "dist",
          minDistance: 10 * 1000,
          maxDistance: 10 * 1000,
          distanceMultiplier: 6371,
          spherical: true,
          key: "location",
        },
      },
    ],
    (err, results) => {
      if (err) {
        res.status(400).json({ error: err });
      } else {
        res.status(200).json({ result: results });
      }
    }
  );
});

This is record inside database :

enter image description here

This is my postman :

enter image description here

correct me if I made any mistake. Thanks

JIGNESH PATEL
  • 367
  • 4
  • 17

0 Answers0