【问题标题】:why does mongodb returns geo index error?为什么mongodb返回地理索引错误?
【发布时间】:2019-10-26 13:48:18
【问题描述】:

我正在构建一个应用程序,用户可以在其中发布工作,我可以加载当前用户附近的工作。从客户端,我发布查询,然后在后端使用这些查询来响应我附近用户的结果。我没有得到结果,而是得到一个索引尚未创建的错误。

我正在使用 postpost 来测试发送查询,这与从客户端执行此操作没有什么不同。

架构文件

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')

路由文件

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
        })
      }
    }
  })
})

我的错误是

{
  "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"
}

如您所见,索引已在模式文件中创建。一旦我传入经度、纬度和距离的查询值以返回我附近的用户,我就会收到此错误。 任何帮助都会非常感谢。

【问题讨论】:

    标签: angular mongodb express mongoose


    【解决方案1】:

    尝试将架构中的 loc 对象更改为有效的 Geojson 对象。像这样:

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

    这是因为2dsphere 索引需要一个有效的 Geojson 对象。 然后,为了查询您的对象,您可以使用来自猫鼬的where()within() 函数,根据 docs

    参考这两个相关问题:12

    【讨论】:

    • 我做了修改,结果还是一样
    • 你能说得更具体点吗?你到底得到了什么错误?你是如何查询数据的?确保索引也直接存在于数据库中。
    猜你喜欢
    • 2013-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-11
    • 1970-01-01
    • 2014-08-28
    • 2021-11-19
    相关资源
    最近更新 更多