在此上下文中不允许猫鼬$ geoNear,$ near和$ nearSphere

问题描述 投票:0回答:1

这是我的架构:

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和$ nearSphere错误。有人知道为什么吗?

这是我的searchQuery:

{ searchQuery: 
{ coordinates: [ 9.993681899999956, 53.5510846 ],
maxDistance: 1000 } }
javascript node.js mongodb express mongoose
1个回答
0
投票

使用count方法代替countDocuments方法

© www.soinside.com 2019 - 2024. All rights reserved.