Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript mongoose$geoNear、$near和$nearSphere在此上下文中是不允许的_Javascript_Node.js_Mongodb_Express_Mongoose - Fatal编程技术网

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报告以了解此问题->