MongoDB - 使用 $geoNear 在使用 maxDistance 时会出现“当查询 GeoJSON 点时, geo near 只接受一个参数”的错误提示。

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 near在查询GeoJSON点时只接受一个参数。发现额外的字段:$maxDistance:100000.0

我看到的大多数讨论建议它是索引问题,但是你可以在user.js中看到我直接调用了它。

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

没有任何运气。

如果有任何建议,我将非常感激!谢谢!

3个回答

7

我也遇到过同样的问题,尝试了很多解决方案,但都没有奏效,真正的问题在于数据格式

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

请确认数据库中的数据和查询指定的点都是正确的格式。

希望能对某些人有所帮助!


4
没错,我的情况是我把坐标输入成了字符串而不是数字。 - Uche Ozoemena

4

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

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

0
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
                    }   
                }])

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接