2016-08-05 108 views
0

我有一個模式,看起來像這樣:貓鼬之內時,查詢或查詢

var UserSchema = new Schema({ 
    name: {type : String, required: true}, 
    location_a: {type: [Number], default: [0,0], index: '2dsphere'}, 
    location_b: {type: [Number], default: [0,0], index: '2dsphere'} 
}); 

我搜索的輸入有一個位置和距離。現在我想讓位於location_a或location_b附近的用戶接近搜索到的位置。

我已經嘗試了查詢,看起來像這樣:

query = query.and([ 
     { $or: [ 
      query.where('location_a').near({ 
       center: {type: 'Point', coordinates: [long, lat]}, 
       maxDistance: distance * 1000, spherical: true 
      }), 
      query.where('location_b').near({ 
       center: {type: 'Point', coordinates: [long, lat]}, 
       maxDistance: 100 * 1000, spherical: true 
      }) 
     ]} 
    ]); 

但它使我的應用程序崩潰與「的RangeError:最大調用堆棧大小超出」

+0

你的''和'沒有做任何事情,而你的'$或'周圍的'[]'括號在不正確的位置 –

+0

你確定[]是問題嗎?我應該在哪裏放置它們? – Vetm

+0

請在顯示查詢前指定查詢的方式 –

回答

0

我使用了錯誤的格式,在我查詢。這將是你如何做到這一點。

query = query.and([ 
     { $or: [ 
      { 
       "at_student_location": { 
        "$near": [12.5386, 41.884], 
        "$maxDistance": 1000 
       } 
      } 
      , 
      { 
       "at_home_location": { 
        "$near": [12.5386, 41.884], 
        "$maxDistance": 1000 
       } 
      } 
     ]} 
    ]); 

的問題是,一個蒙戈只接受每個查詢一個geoNear表達式所以這是不可能做到這一點。