1

I am building an app in which users can post job and I can load up the jobs that are near the current user. From the client side, I post queries that are then used on the backend to respond with the results of users near me. Instead of getting the result, I get an error that the index has not been created.

I am using postpost to test sending the queries, it's no different than doing it from the client side.

schema file

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

const userSchema = new schema({
  username: String,
  password: String,
  first_name: String,
  last_name: String,
  email: String,
  phone_number: Number,
  zip_code: Number,
  loc: {
    type: {
      type: String,
      default: "Point"
    },
    coordinates: []
  }
})
userSchema.index({
  'loc': '2dsphere'
});

module.exports = mongoose.model('user', userSchema, 'users')

routes file

const express = require('express')
const router = express.Router();
const mongoose = require('mongoose')
const jwt = require('jsonwebtoken')
const User = require('../models/user')
const db = 'mongodb+srv://georges:<1997>@cluster0-rm0vl.mongodb.net/users?retryWrites=true&w=majority'

mongoose.connect(db, {
  useNewUrlParser: true
}, (err) => {
  if (err) {
    console.log('The error is' + err)
  } else {
    console.log('the databasee is connected')
  }
})

router.get('/nearme/', (req, res) => {
  let queries = req.query;
  var distance = parseInt(req.query.maxDistance)
  var long = parseInt(req.query.long)
  var lat = parseInt(req.query.lat)
  User.find({
    loc: {
      $near: {
        $geometry: {
          type: "Point",
          coordinates: [long, lat]
        },
        $maxDistance: 10.5
      }
    }
  }, function(error, usernearme) {
    if (error) {
      res.status(401).send(error)
    } else {
      if (usernearme.length === 0) {
        res.status(401).send('no user near me')
      } else {
        res.status(200).send({
          usernearme
        })
      }
    }
  })
})

my error is

{
  "operationTime": "6701513149272555521",
  "ok": 0,
  "errmsg": "error processing query: ns=users.usersTree: 
  GEONEAR field = loc maxdist = 10.5 isNearSphere = 0\ nSort: {}\
  nProj: {}\
  n planner returned error: unable to find index
  for $geoNear query ",
  "code": 2,
  "codeName": "BadValue",
  "$clusterTime": {
    "clusterTime": "6701513149272555521",
    "signature": {
      "hash": "tsfDSZp+cZIi6WDoULsy/NH+BQ0=",
      "keyId": "6696267117303431169"
    }
  },
  "name": "MongoError"
}

and as you can see the index is already created in the schema file. I get this error once I've passed in the query values of longitude, latitude, and distance to return a user near me. Any help would be great thank you.

Vikash_Singh
  • 1,856
  • 2
  • 14
  • 27
georges
  • 17
  • 1
  • 7

1 Answers1

0

Try to change you loc object in the schema to a valid Geojson object. Like this:

loc {
  geometry: {
    type: { type: String, default: 'Point' } , 
    coordinates: {type: [Number] }
    } 
} 

This is because the 2dsphere index expects a valid Geojson object. Then, for querying your objects, you could use the where() and within() functions from mongoose, as per the docs.

For reference see these two related questions: 1 and 2

philoez98
  • 493
  • 4
  • 13