为什么mongodb返回地理索引错误? [英] why does mongodb returns geo index error?
问题描述
我正在构建一个应用程序,用户可以在其中发布职位,并且可以加载当前用户附近的职位.在客户端,我发布查询,然后在后端使用这些查询以响应我附近的用户的结果.没有得到结果,而是得到一个错误,指出尚未创建索引.
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.
我正在使用postpost来测试发送查询的过程,这与从客户端进行操作没什么不同.
I am using postpost to test sending the queries, it's no different than doing it from the client side.
模式文件
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"
}
,如您所见,索引已在模式文件中创建.传递经度,纬度和距离的查询值以返回附近的用户时,会出现此错误. 任何帮助都将非常感谢.
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.
推荐答案
尝试将架构中的 loc 对象更改为有效的Geojson对象.像这样:
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] }
}
}
这是因为 2dsphere 索引要求有效的Geojson目的.
然后,根据where()和within()
函数来查询对象. >文档.
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.
作为参考,请参见以下两个相关问题: 1 和
For reference see these two related questions: 1 and 2
这篇关于为什么mongodb返回地理索引错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!