javascript - 在此上下文中不允许使用 mongoose $geoNear、$near 和 $nearSphere
问题描述
这是我的架构:
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});
};
})
})
})
我在这篇文章中这样做了:https ://medium.com/@galford151/mongoose-geospatial-queries-with-near-59800b79c0f6
但我得到错误 $geoNear, $near, and $nearSphere are not allowed in this context。有人知道为什么吗?
这是我的搜索查询:
{ searchQuery:
{ coordinates: [ 9.993681899999956, 53.5510846 ],
maxDistance: 1000 } }
解决方案
使用 count 方法而不是 countDocuments 方法
推荐阅读
- java - 收到高优先级通知时如何使手机显示为透明黑色?
- android-studio - 使用 Android Studio IDE 是唯一可以使用 Capacitor 3 构建签名 APK 的吗?
- visual-studio-code - 如何更喜欢突出内括号而不是外括号?
- java - 为什么我在尝试使用 mockMvc 测试控制器时不能模拟控制器的服务依赖关系?
- javascript - 我想将数据 js 传输到 node.js
- python - 如何用混合部分分割线
- webpack - 使用 css-loader 6 加载语义 ui-css 的问题
- python - 格式化多维数组 Python
- python - 如果满足条件,则从 CSV 文件中删除行
- reactjs - 为什么在 webpack 的输出属性的入口点和绝对路径中使用相对路径?