node.js - mongodb - 使用 $geoNear 在使用 maxDistance 时给出 "geo near accepts just one argument when querying for a GeoJSON point"

标签 node.js mongodb mongoose geojson geonear

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

// 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: Failed to determine whether query system can provide a covered projection :: caused by :: geo near accepts just one argument when querying for a GeoJSON point. Extra field found: $maxDistance: 100000.0

我看到的大多数线程都表明这是索引问题,但您可以在 user.js 中看到我直接调用

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

没有任何运气。

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

最佳答案

我遇到了同样的问题并经历了许多解决方案,但其中任何一个都没有奏效 真正的问题在于数据

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

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

希望它对某人有所帮助!

关于node.js - mongodb - 使用 $geoNear 在使用 maxDistance 时给出 "geo near accepts just one argument when querying for a GeoJSON point",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65205315/

相关文章:

node.js - 解密 AES256 密码时出现错误 "wrong final block length"

javascript - Nodejs 如何在 Controller 中动态操作 MongoDB 聚合?

node.js - Express + TypeScript : Create type inference for response. 本地人

node.js - 咕噜声不工作 : -bash: exec: grunt: not found

javascript - 调用方法失败

mongodb - 将本地 mongo 集合部署到 meteor.com 服务器

angularjs - Forms-Angular,自定义指令字段如何影响完整记录的 $pristine 状态?

angularjs - 登录后让用户返回上一页( Node )

javascript - 在 Javascript (node.js) 中的 1, 4, 7... 处插入 div.row

node.js - GridFS:清除所有未引用的文件