【问题标题】:Mongoose unable to perform $near query on array of locationsMongoose 无法对位置数组执行 $near 查询
【发布时间】:2018-06-21 08:50:16
【问题描述】:

我已经尝试寻找解决方案,并且我已经阅读了 $near 和 $geoNear 查询的部分文档,但我没有成功。

var userSchema = new Schema({
  zipcode : {
    formatted : {type : String, required : false, default : ""},
    geo : { type: [Number], default: [0,0]} // long, lat
  }
});
userSchema.index({"zipcode.geo" : '2d'});

在不同的文件上

var locationSchema = new Schema({
  formatted : {type : String, unique : false, required : true},
  geo : { type: [Number], default: [0,0]}
});

locationSchema.index({geo: '2d'});

var storeSchema = new Schema({
  locations : [locationSchema],
});

在我的控制器上

let nearQuery = {
  $near : [ user.zipcode.geo[0], user.zipcode.geo[1]],
  spherical: true,
  $maxDistance: 7000
}
Stores.find(locations : nearQuery).exec(function(error, doc) {

});

但我最终得到了错误 - “错误:不能将球形与 Array 一起使用。”

当我删除时

spherical : true

我收到错误 - “规划器返回错误:无法找到 $geoNear 查询的索引”

我知道我做错了什么,我只是不知道如何解决它。我怎样才能解决这个问题?或者这样做的最佳方法是什么?

提前谢谢你。

【问题讨论】:

    标签: node.js mongodb mongoose geolocation


    【解决方案1】:

    原来我对查询使用了错误的语法,并试图在位置字段上运行查询,而我应该在位置.geo 字段上完成它。

    https://docs.mongodb.com/manual/tutorial/query-a-2d-index/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-13
      • 1970-01-01
      相关资源
      最近更新 更多