【问题标题】:mongodb - using $geoNear gives "geo near accepts just one argument when querying for a GeoJSON point" when using maxDistancemongodb - 在使用 maxDistance 时,使用 $geoNear 会给出“在查询 GeoJSON 点时,geo near 只接受一个参数”
【发布时间】:2021-03-20 03:38:34
【问题描述】:

这似乎是一个常见错误,但我似乎无法使其适用于我所看到的所有建议。这是我的设置:

// 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' });

运气不好。

非常感谢任何建议,谢谢!

【问题讨论】:

    标签: node.js mongodb mongoose geojson geonear


    【解决方案1】:

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

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

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

    希望对某人有所帮助!

    【讨论】:

    • 是的,这是真的。就我而言,问题是我将坐标作为字符串而不是数字提供。
    【解决方案2】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-04-23
      • 2012-11-29
      • 2014-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多