简体   繁体   English

猫鼬无法对位置数组执行$ near查询

[英]Mongoose unable to perform $near query on array of locations

I've tried looking for solutions and I've read parts of the documentation for the $near and $geoNear queries but I have not been successful. 我尝试寻找解决方案,并且阅读了$ 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'});

ON A DIFFERENT FILE 在不同的文件上

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],
});

On my controller 在我的控制器上

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

});

But i end up getting the error - "Error: Can't use spherical with Array." 但是我最终遇到了错误-“错误:无法将球面阵列用于Array。”

When I remove 当我删除

spherical : true

I get the error - " planner returned error: unable to find index for $geoNear query" 我收到错误消息-“计划人员返回了错误:找不到$ geoNear查询的索引”

I know i am doing something wrong, I just cant figure out how to fix it. 我知道我做错了,我只是想不出办法来解决。 How can I fix this? 我怎样才能解决这个问题? or What is the best way of doing this? 或最佳方法是什么?

Thank you in advance. 先感谢您。

Turns out I was using the wrong syntax for the query and was trying to run the query on the locations field, while I should have done it on the locations.geo field. 原来,我对查询使用了错误的语法,并试图在locations字段上运行查询,而我应该在locations.geo字段上执行查询。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM