Javascript mongoose$geoNear、$near和$nearSphere在此上下文中是不允许的
这是我的模式:Javascript mongoose$geoNear、$near和$nearSphere在此上下文中是不允许的,javascript,node.js,mongodb,express,mongoose,Javascript,Node.js,Mongodb,Express,Mongoose,这是我的模式: const userSchema = mongoose.Schema({ coordinates: { type: { type: String }, coordinates: [], }, }) 然后我补充说: userSchema.index({ coordinates: "2dsphere" }); 我想找到半径内的所有坐标。这是我正在调用的方法: router.post('/users/query', async(
const userSchema = mongoose.Schema({
coordinates: {
type: { type: String },
coordinates: [],
},
})
然后我补充说:
userSchema.index({ coordinates: "2dsphere" });
我想找到半径内的所有坐标。这是我正在调用的方法:
router.post('/users/query', async(req,res)=>{
var pageOptions = {
page: req.body.page || 0,
limit: 10
}
const query ={ location: {
$near: {
$maxDistance: req.body.searchQuery.maxDistance,
$geometry: {
type: "Point",
coordinates: req.body.searchQuery.coordinates
}
}
}}
console.log(query)
User.find(query)
.skip(pageOptions.page*pageOptions.limit)
.limit(pageOptions.limit)
.exec(function (error, users) {
if(error) { res.status(500).json({error}); return; };
User.countDocuments(query).exec((error,total)=>{
if(error) {
res.status(500).json({error})
}else{
res.status(200).json({users,total});
};
})
})
})
我在这篇文章中是这样做的:
但是我得到错误$geoNear、$near和$nearSphere在这个上下文中是不允许的。有人知道为什么
这是我的搜索查询:
{ searchQuery:
{ coordinates: [ 9.993681899999956, 53.5510846 ],
maxDistance: 1000 } }
使用计数方法而不是countDocuments方法使用计数方法而不是countDocuments方法这不是一个很好的答案。count已弃用,应使用.countDocuments。要获得正确的解决方案,请查看MongoDB论坛上的bug报告,了解这个问题->这不是一个很好的答案。count已弃用,应使用.countDocuments。要获得正确的解决方案,请查看MongoDB论坛上的bug报告以了解此问题->