mongodb - 使用 $geoNear 在使用 maxDistance 时给出“在查询 GeoJSON 点时,geo close 仅接受一个参数”

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

这似乎是一个常见错误,但我似乎无法使其与我见过的所有建议一起工作。这是我的设置:

// point.js (based on mongoose recommended subdocument pattern for reusing the GeoJSON definition
// see here https://mongoosejs.com/docs/geojson.html)
const mongoose = require('mongoose');

const pointSchema = new mongoose.Schema({
    type: {
        type: String,
        enum: ['Point'],
        required: true
    },
    coordinates: {
        type: [Number],
        required: true,
    }
});

exports = pointSchema;
// user.js
var schema = new Schema({
  location: {
    type: pointSchema,
  },
  ...
});

schema.index({ location: '2dsphere' });
var User = mongoose.model('User', schema);
// routeHandler.js
          const near = { type: 'Point', coordinates: [lng, lat] };
          User.aggregate([
            { $geoNear: {
              near,
              distanceField: 'dist',
              maxDistance: 100000,
              spherical: true,
            } },
            ...
          ]).exec((err, results) => {
            console.log('error or results:', err, results);
          });

我收到此错误:

MongoError:无法确定查询系统是否可以提供由 :: geo close 引起的覆盖投影 :: 查询 GeoJSON 点时仅接受一个参数。发现额外字段:$maxDistance:100000.0

我见过的大多数线程都表明这是索引问题,但您可以在

user.js
中看到我直接调用

schema.index({ location: '2dsphere' });

没有任何运气。

我真的很感激任何建议,谢谢!

node.js mongodb mongoose geojson geonear
4个回答
7
投票

我遇到了同样的问题,并尝试了很多解决方案,但其中任何一个都不起作用 真正的问题在于数据

在我的模型中有 2dsphere 索引,这是正确的,但使用 2dshpere 时数据库中的位置应采用位置格式:[ lng,lat ],这是主要问题。因为 mongodb 不会验证这种格式的位置数组。

验证数据库中的数据和查询时指定的点都应该是正确的格式。

希望对某人有帮助!


5
投票

我也遇到了同样的错误,经过大量研究后我发现运行聚合查询的错误。我将坐标作为字符串数组传递,它应该是数字数组。

{
        $geoNear: {
          near: {
            type: "Point",
            coordinates: [103.83797, 1.46103],
          },
          distanceField: "dist.calculated",
          maxDistance: distance,
          includeLocs: "dist.location",
          spherical: true,
        },
      },

1
投票
issue :  caused by :: geo near accepts just one argument when querying for a GeoJSON point. Extra field found: $maxDistance: 1000.0

Solution : i got this issue because i passed latitude and longitude in string format through postman, and directly passed into query parameter, SO i changed it into number and it's resolved.

    searchData = await ServiceIndividualModel.aggregate([{
                    $geoNear: {
                        near: {
                            type: "Point",
                            coordinates: [Number(longitude), Number(latitude)]
                        },
                       distanceField: "distance",
                       maxDistance: 1000,
                       spherical: true
                    }   
                }])

0
投票

邮递员会将数字转换为字符串,添加此 $geoNear:{ 靠近: { 类型:“点”, 坐标:[数字(经度),数字(纬度)] },

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