node.js - MongoError :"geoNear command failed: { ok: 0.0, errmsg:\"错误处理查询","code":16604 ,"codeName" :"Location16604"

标签 node.js mongodb mongoose aggregate geonear

我对 mongodb geonear 聚合查询感到沮丧,对于每个响应我都会收到这样的错误:

{"name":"MongoError","message":"geoNear command failed: { ok: 0.0, errmsg: \"error processing query: ns=Lab.assoprofiles limit=100Tree: GEONEAR  field=loc maxdist=50000 isNearSphere=1\nSort: {}\nProj: { $pt: { $meta: \"geoNearPoin...\", code: 2, codeName: \"BadValue\" }","ok":0,"errmsg":"geoNear command failed: { ok: 0.0, errmsg: \"error processing query: ns=Lab.assoprofiles limit=100Tree: GEONEAR  field=loc maxdist=50000 isNearSphere=1\nSort: {}\nProj: { $pt: { $meta: \"geoNearPoin...\", code: 2, codeName: \"BadValue\" }","code":16604,"codeName":"Location16604"}

我是这样设计这个系列的:

const mongoose = require("mongoose");
var Schema = mongoose.Schema;

var AssoSchema = new mongoose.Schema({
   userId:{ type: mongoose.Schema.Types.ObjectId, ref: "User"},
   picture : String,
   telephone: {
      type: String,
      unique: false
   },
   loc: {
      type: [Number], // [<longitude>, <latitude>]
      index: "2d" // create the geospatial index
   },

},{timestamps: true})

var Asso = mongoose.model('AssoProfile', AssoSchema);
module.exports = Asso;

查询是这样的:

const latitude = parseFloat(req.body.latitude);
const longitude = parseFloat(req.body.longitude);
AssoProfile.aggregate([
     {
        $geoNear: {
           near: { type: 'Point', coordinates: [latitude, longitude] },
           spherical: true, distanceField: 'distance', maxDistance:7000,
        }
     }

   ], function(err, l ){
      console.log(JSON.stringify(err));
      console.log(l);
   })

我不明白为什么这么简单的查询会抛出这个错误。 感谢您的帮助。

最佳答案

我已经运行了你在上面发布的相同代码,但它对我不起作用......而且我认为 index: 2d 在 mongoose 模型中不是一个选项.相反,我创建了这样的索引并为我工作。

const mongoose = require("mongoose")
var Schema = mongoose.Schema

var AssoSchema = new mongoose.Schema({
   userId: { type: mongoose.Schema.Types.ObjectId, ref: "User" },
   picture : String,
   telephone: {
      type: String,
      unique: false
   },
   loc: Array,

}, { timestamps: true })


AssoSchema.index({ 'loc': '2dsphere' })

var Asso = mongoose.model('AssoProfile', AssoSchema)
module.exports = Asso;

关于node.js - MongoError :"geoNear command failed: { ok: 0.0, errmsg:\"错误处理查询","code":16604 ,"codeName" :"Location16604",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51812714/

相关文章:

node.js - @package和node_modules中的package有什么关系

javascript - 访问 Node 中的构造函数变量。 js

mongodb - docker备份和恢复mongodb

mongodb - Mongoose 使用 key 文件进行身份验证

node.js - Linux Node 模块版本不匹配

unix - 须藤路径 - 没有找到 Node.js

linux - kubectl exec 行为异常,即使二进制文件在 pod 中,也未找到命令

java.security.cert.CertPathValidatorException : Certificate chaining error

javascript - Mongoose :在一次调用中填充多个查询

node.js - Node js Mongoose 使用 mongoose-encryption 加密数据