簡體   English   中英

貓鼬無法對位置數組執行$ near查詢

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

我嘗試尋找解決方案,並且閱讀了$ 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查詢的索引”

我知道我做錯了,我只是想不出辦法來解決。 我怎樣才能解決這個問題? 或最佳方法是什么?

先感謝您。

原來,我對查詢使用了錯誤的語法,並試圖在locations字段上運行查詢,而我應該在locations.geo字段上執行查詢。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM